Trying to get a JSONObect to tokener help please ?
I need to get some information from an JSONArray and i thought tokener would do it. Heres my code
try{
JSONArray jArray = new JSONArray(r开发者_JS百科esult);
for(int i=0;i<jArray.length();i++){
String data = jArray.getString(i);
JSONObject json_data = (JSONObject)new JSONTokener (data).nextValue();
String query = json_data.getString("name");
JSONArray locations = json_data.getJSONArray("locations");
//Get an output to the screen
txt.setText(query);
}
The actual array is filling with data from my database via http post.
Field names are id
, name
, servings
, description
.
If your JSONArray contains JSON objects, than you could do:
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.optJSONObject(i)
// making sure we got JSON object
if(json_data == null) continue;
String query = json_data.getString("name");
JSONArray locations = json_data.getJSONArray("locations");
//Get an output to the screen
txt.setText(query);
}
Or you could use Gson to handle JSON (de)serialization. You don't have to write custom code for this. http://code.google.com/p/google-gson/
精彩评论