Kyriakos Geroudes
Kyriakos Geroudes

Reputation: 37

How can I access JSONArray elements in JSP

my JSONArray is

[
   {"id":1,
    "programs_idprograms":3,
    "description":"course description",
    "idCourse":"course title"
   },
   {"id":2,
    "programs_idprograms":3,
    "description":"course description2",
    "idCourse":"course title2"
   }
]  

is stored in a string called bean2

Upvotes: 2

Views: 6009

Answers (2)

betteroutthanin
betteroutthanin

Reputation: 7556

JSONArray array = new JSONArray(bean2);
for(int i=0;i<array.length();i++){
    JSONObject object = array.getJSONObject(i);
    int id = object.getInt("id");
    int programs_idprograms = object.getInt("programs_idprograms");
    String description = object.getString("description");
    String idCourse = object.getString("idCourse");   
}

Upvotes: 1

sandipon
sandipon

Reputation: 986

Try it:

jsonArray = [ {"id":1,
"programs_idprograms":3,
"description":"course description",
"idCourse":"course title"
},  ... ];

One way to do it is to construct the html in Javascript code like this:

 var myHTMLStr = '<table>';
 for(var i in jsonArray) {
 myHTMLStr+='<tr><td>' + jsonArray[i]['id'] + '</td><td>' + jsonArray[i]  ['programs_idprograms'] +   '</td></tr>';

 }
myHTMLStr+='</table>';

Now display the HTML string:

document.getElementById('tableOutput').innerHTML = myHTMLStr;

Upvotes: 1

Related Questions