简体   繁体   中英

How to parse this JSON, android?

I have the below 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"
    ]
  }
}
]
 }
}

I need to get "screenshots" tag array. This is my code

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);
                    }
                }

jsonObj contain json value

    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]);
         }
     }

you can try like this

Assuming you already have setup all the prerequisites for json parsing in android.

I will move towards how to parse this data

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();
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM