开发者

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜