如何解析这个JSON,android?

时间:2015-04-10 12:49:13

标签: java android json

我有以下JSON

{"data":{
"result": true,
"applications": [
{
  "application_name": "Hill Rider Game",
  "application_os": "Android",

  "app_downloads": "1,000,000+",
  "bundle_id": "com.touchfoo.swordigo",
  "playstore": {
    "downloads": "1,000,000 - 5,000,000",
    "rating": 4.3144540786743,
    "developer": "Touch Foo",
    "screenshots": [
      "https://lh4.ggpht.com/B8xLNXsT_OOezuHDMtX1ZqRgEX_D8NV79bktaLpJ41Rh9Ng8dJ_vC35mEmXiT4iOrhc=h310",
      "https://lh6.ggpht.com/VCWp2ltqspB2qjznCeq9hhMwdG10nxYfg2URbH1_F8cmdtkW-MLCS0aJUnZ-IvnNXg=h310",
      "https://lh4.ggpht.com/DJqmDOgAqC_COJI5AVTxWeehcM6B51bFkedVRIG0iQdrxsHf42Dy0-NPDfw5nhnXDEOB=h310",
      "https://lh5.ggpht.com/E0VlpwiBGTBPnCpzekxv6kJG1FCaPQENuxXi1OUu9jGv7Ib-vc4j4AkaGugz7bjhg94-=h310"

    ]
  }
},
{
  "application_name": "Appprix Testing",
  "application_os": "Android",

  "app_downloads": "10,000,000+",
  "bundle_id": "com.creativemobile.dragracingbe",
  "playstore": {
    "downloads": "10,000,000 - 50,000,000",
    "rating": 4.2277860641479,
    "developer": "Creative Mobile",
    "screenshots": [
      "https://lh6.ggpht.com/mZxezzFUAyRz7g2zcULV8vLILrMs6yuP_Yux60F3DPA7x89TIlLzgWkDgyQFhugAMQc=h310",
      "https://lh4.ggpht.com/iapxrJoX2u-O3wvh9i9J6L4_tmyoflj2OGbZCe8xdJiqb8EE8yIH3lzE1h0UinfqDQ=h310",
      "https://lh5.ggpht.com/YgDWaxt00LBEd-5p-AfVhAxf7u45m82mD2Uk44wkQm0QBCxEMYX4rrUaWDaS9PEvTSE=h310",
      "https://lh6.ggpht.com/k5ixEniw9F6sZ-B0HU2ckEHg9tBsjMz45xZ2LX3-hcjLcFTT4_VZfTBkbqVYDXnlYSQ=h310"
    ]
  }
},
{
  "application_name": "Hill Climb SteamPunk",
  "application_os": "Android",

  "app_downloads": "1,000,000+",
  "bundle_id": "com.touchfoo.swordigo",
  "playstore": {
    "downloads": "1,000,000 - 5,000,000",
    "rating": 4.3144540786743,
    "developer": "Touch Foo",
    "screenshots": [
      "https://lh4.ggpht.com/B8xLNXsT_OOezuHDMtX1ZqRgEX_D8NV79bktaLpJ41Rh9Ng8dJ_vC35mEmXiT4iOrhc=h310",
      "https://lh6.ggpht.com/VCWp2ltqspB2qjznCeq9hhMwdG10nxYfg2URbH1_F8cmdtkW-MLCS0aJUnZ-IvnNXg=h310",
      "https://lh4.ggpht.com/DJqmDOgAqC_COJI5AVTxWeehcM6B51bFkedVRIG0iQdrxsHf42Dy0-NPDfw5nhnXDEOB=h310",
      "https://lh5.ggpht.com/E0VlpwiBGTBPnCpzekxv6kJG1FCaPQENuxXi1OUu9jGv7Ib-vc4j4AkaGugz7bjhg94-=h310"
    ]
  }
},
{
  "application_name": "abcd",
  "application_os": "Android",

  "app_downloads": "10,000+",
  "bundle_id": "com.piggypop.cricket",
  "playstore": {
    "downloads": "10,000 - 50,000",
    "rating": 3.9359374046326,
    "developer": "Piggy Pop",
    "screenshots": [
      "https://lh3.ggpht.com/vvpskwV3TfsrzkmLXF4_NIEY-fzKYFjiXtpF8C2ovS6mJlfYG4dkGAYsSWHbv1lzmw=h310",
      "https://lh5.ggpht.com/RrfwHr3lEfaqOtg_Hoa30EMuP9cS_GW4fAeZH_W3SCcubi1IiqV5OpuyMTWT8gPbwQ=h310",
      "https://lh4.ggpht.com/WdivGfxtUAKPxhMGayfLD5egCm3y0mz8RBQcyCnQBhuHAE_a5deZmAv4evkwGny6eHQ=h310",
      "https://lh5.ggpht.com/sbLzAUgprB8vQkqLMgqdAz2TyI5az572scxVuPoHa8I2zk7B5euCebx3ECr4iOHLxvA=h310",
      "https://lh5.ggpht.com/6Vk9DTDY4Grcn_OaiaJmEuaR2fi94H55PyEYg9NhbPAoxjRnMgt7oZBaA1SQLVfSj1w=h310"
    ]
  }
}
]
 }
}

我需要获取“screenshots”标签数组。这是我的代码

JSONArray responseArray = jsonObj.getJSONArray("body");
for (int i = 0; i < responseArray.length(); i++) {
JSONArray applicationsArray = c.getJSONArray("applications");
                for (int k = 0; k < applicationsArray.length(); k++) {
                    JSONObject e = applicationsArray.getJSONObject(k);

                    JSONArray playstore = e.getJSONArray("playstore");
                    for (int l = 0; l < playstore.length(); l++) {
                        JSONObject f = playstore.getJSONObject(l);
                        Log.e("scren ",""+f.getString("screenshots"));
                        JSONArray screenshotArray = f.getJSONArray("screenshots");
                        Log.e("scren ",""+screenshotArray);
                    }
                }

2 个答案:

答案 0 :(得分:3)

jsonObj包含json值

    JSONObject data = jsonObj.getJSONObject("data");
    JSONArray applications = data.getJSONArray("applications");

     for (int k = 0; k < applications.length(); k++) {
         JSONObject playstore = applications[k].getJSONObject("playstore");
         JSONArray screenshots = playstore.getJSONArray("screenshots");
         for (int i = 0; i < screenshots.length(); i++) {
            Log.e("screenshot  ",""+screenshots[i]);
         }
     }

你可以这样尝试

答案 1 :(得分:2)

假设您已经在android中设置了json解析的所有先决条件。

我将转向如何解析此数据

if(response!=null){
            try {

                JSONObject jsonObject = new JSONObject(response);
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray jsonArray = jsonObject.optJSONArray("applications");
                for(int i=0; i<jsonArray.length(); i++){
                    jsonObject= jsonArray.getJSONObject(i);



                    JSONObject c = jsonArray.getJSONObject(i);

                    String app_name = c.getString("application_name");
                    String app_os = c.getString("application_os");
                    String app_downloads = c.getString("app_downloads");
                    String bundle = c.getString("bundle_id");


                    // playstore node is JSON Object
                    JSONObject playstore = c.getJSONObject("playstore");
                    String downloads = phone.getString("downloads");
                    String rating = phone.getString("rating");
                    String developer = phone.getString("developer");

                    // screenshots node is JSON Array
                    JSONArray screenshots = jsonObject.optJSONArray("screenshots");
                    for(int j=0; j<screenshots.length(); j++){
                        Log.e("screenshot  ",""+screenshots[i]);

                    }

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }