Android在另一个对象中解析JSON对象

时间:2015-03-28 08:34:17

标签: android json object

我尝试在Android中的另一个对象中解析JSON对象。

{
  "data": [
    {
      "id": "1", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "1", 
        "name": "name1"
            }
    }, 

    {
      "id": "2", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "2", 
        "name": "name2"
            } 
    }, 

    {
      "id": "3", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "3", 
        "name": "name3"
            }
    }
  ]
}

和我的Android代码

// OnCreate中

new JSONAsyncTask().execute("http://bomaza.com/test/json.php");

// doInBackground

@Override
protected Boolean doInBackground(String... urls) {
    try {
        HttpGet httppost = new HttpGet(urls[0]);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httppost);
        int status = response.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);

            JSONObject jsono = new JSONObject(data);
            JSONArray jarray = jsono.getJSONArray("data");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                Actors actor = new Actors();

                actor.setName(object.getString("id"));
                actor.setDesc(object.getString("name"));
                actor.setImage(object.getString("picture"));

                actorsList.add(actor);
            }
            return true;
        }

        //------------------>>

    } catch (ParseException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}

我遇到了Child对象解析问题&#34; name&#34;提交不解析我怎么能解决它?内部对象(&#34;来自&#34;)没有解析。

我想解析&#34;图片&#34;和&#34;名称&#34;。

1 个答案:

答案 0 :(得分:1)

试试这段代码。希望它适合你。

Actors actor = new Actors();
actor.setName(object.getString("id"));
actor.setImage(object.getString("picture"));  
JSONObject object1 = object.getJSONObject("from");
actor.setDesc(object1.getString("name"));