how to get all json object

对着背影说爱祢 提交于 2019-12-12 03:52:32

问题


I find so many answer but it indicate the string and i want all jsonobjects LINK1 LINK2

Here is my json

         {
            timestamp: 1471845537300,
            list: {
            724206: {
            id: "724206",
            name: "HINGIS M. (SUI)",
            points: "10790",
            ranking: "1",
            tour: "WTA-D",
            lastUpdate: "2016-08-15"
            },
            724207: {
            id: "724207",
            name: "MIRZA S. (IND)",
            points: "10790",
            ranking: "2",
            tour: "WTA-D",
            lastUpdate: "2016-08-15"
            },
            724208: {
            id: "724208",
            name: "GARCIA C. (FRA)",
            points: "6210",
            ranking: "3",
            tour: "WTA-D",
            lastUpdate: "2016-08-15"
            },
            724209: {
            id: "724209",
            name: "MLADENOVIC K. (FRA)",
            points: "6045",
            ranking: "4",
            tour: "WTA-D",
            lastUpdate: "2016-08-15"
            },
    type: "TENNIS RANKING"

            }
}

i added a screenshot so u can batter understand

I tried this.

 public class GetData extends AsyncTask<String, Void, Void> {

        String responseString;
        Response response;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... str) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("MY url")
                    .build();

            try {

                response = client.newCall(request).execute();
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                {
                    responseString = response.body().string();
                    System.out.println(responseString);
                    response.body().close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (responseString != null) {

                try {
                    JSONObject jObject = new JSONObject(responseString).getJSONObject("list");
                    Iterator<String> keys = jObject.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        Log.v("**********", "**********");
                        Log.v("list key", key);
                        JSONObject innerJObject = jObject.getJSONObject(key);
                        Iterator<String> innerKeys = innerJObject.keys();
                        while (innerKeys.hasNext()) {
                            String innerKkey = keys.next();
                            String value = innerJObject.getString(innerKkey);
                            Log.v("key = " + key, "value = " + value);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }else{
                Toast.makeText(getActivity(), "Couldn't get response", Toast.LENGTH_SHORT).show();
            }

        }
    }

LOGCAT:

   08-22 15:11:51.222 12592-12592/com.bicubic.tennis V/list key: type
08-22 15:11:51.222 12592-12592/com.bicubic.tennis V/key = type: value = TENNIS RANKING
08-22 15:11:51.222 12592-12592/com.bicubic.tennis V/list key: 725406
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725406, name = NICOLAS MAHUT (FRA), points = 9475, ranking = 1, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725407
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725407, name = PIERRE-HUGUES HERBERT (FRA), points = 9085, ranking = 2, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725408
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725408, name = MARCELO MELO (BRA), points = 7620, ranking = 3, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725409
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725409, name = JAMIE MURRAY (GBR), points = 6805, ranking = 4, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725410
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725410, name = BOB BRYAN (USA), points = 5750, ranking = 5, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725411
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725411, name = MIKE BRYAN (USA), points = 5750, ranking = 6, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725412
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725412, name = IVAN DODIG (CRO), points = 5630, ranking = 7, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725413
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725413, name = BRUNO SOARES (BRA), points = 5480, ranking = 8, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725414
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725414, name = HORIA TECAU (ROU), points = 5420, ranking = 9, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725415
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725415, name = RAVEN KLAASEN (RSA), points = 5120, ranking = 10, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725416
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725416, name = JEAN-JULIEN ROJER (NED), points = 4810, ranking = 11, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725417
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725417, name = DANIEL NESTOR (CAN), points = 4440, ranking = 12, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725418
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725418, name = EDOUARD ROGER-VASSELIN (FRA), points = 4350, ranking = 13, tour = ATP-D, lastUpdate = 2016-08-22
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/list key: 725419
08-22 15:11:51.223 12592-12592/com.bicubic.tennis V/details: id = 725419, name = VASEK POSPISIL (CAN), points = 4330, ranking = 14, tour = ATP-D, lastUpdate = 2016-08-22

回答1:


Provided your JSON is like this,

{
    "timestamp": 1471845537300,
    "list": {
        "724206": {
            "id": "724206",
            "name": "HINGIS M. (SUI)",
            "points": "10790",
            "ranking": "1",
            "tour": "WTA-D",
            "lastUpdate": "2016-08-15"
        },
        "724207": {
            "id": "724207",
            "name": "MIRZA S. (IND)",
            "points": "10790",
            "ranking": "2",
            "tour": "WTA-D",
            "lastUpdate": "2016-08-15"
        },
        "724208": {
            "id": "724208",
            "name": "GARCIA C. (FRA)",
            "points": "6210",
            "ranking": "3",
            "tour": "WTA-D",
            "lastUpdate": "2016-08-15"
        },
        "724209": {
            "id": "724209",
            "name": "MLADENOVIC K. (FRA)",
            "points": "6045",
            "ranking": "4",
            "tour": "WTA-D",
            "lastUpdate": "2016-08-15"
        },
        "type": "TENNIS RANKING"
    }
}

NOTE : I was not sure if type is outside or inside the list object. I considered it to be inside the list object (which shouldn't be the case usually).

Here is a Quick and dirty solution on how to parse this, You can use the instanceof keyword to check if the data is a String or a JSONObject. The problem is if you get something other than a String or a JSONObject it will not be parsed.

try {
    JSONObject jObject = new JSONObject(responseString).getJSONObject("list");
    Iterator<String> keys = jObject.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        Log.v("list key", key);
        if(jObject.get(key) instanceof JSONObject) {
            JSONObject innerJObject = jObject.getJSONObject(key);
            String id = innerJObject.getString("id");
            String name = innerJObject.getString("name");
            String points = innerJObject.getString("points");
            String ranking = innerJObject.getString("ranking");
            String tour = innerJObject.getString("tour");
            String lastUpdate = innerJObject.gettString("lastUpdate");
            Log.v("details", "id = " + id + ", " + "name = " + name + ", " + "points = " + points + ", " + "ranking = " + ranking + ", " + "tour = " + tour + ", " + "lastUpdate = " + lastUpdate);
        } else if (jObject.get(key) instanceof String){
            String value = jObject.getString("type");
            Log.v("key = type", "value = " + value);
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Output,

list key: 724207
details: id = 724207, name = MIRZA S. (IND), points = 10790, ranking = 2, tour = WTA-D, lastUpdate = 2016-08-15
list key: 724208
details: id = 724208, name = GARCIA C. (FRA), points = 6210, ranking = 3, tour = WTA-D, lastUpdate = 2016-08-15
list key: 724209
details: id = 724209, name = MLADENOVIC K. (FRA), points = 6045, ranking = 4, tour = WTA-D, lastUpdate = 2016-08-15
list key: type
key = type: value = TENNIS RANKING
list key: 724206
details: id = 724206, name = HINGIS M. (SUI), points = 10790, ranking = 1, tour = WTA-D, lastUpdate = 2016-08-15



回答2:


First of all your json is incorrect as you can validate it on jsonlint

http://www.jsonlint.com/

this is what i am facing while validating your json

Error: Parse error on line 1:
 {  timestamp: 147184553
----^
Expecting 'STRING', '}', got 'undefined'



回答3:


You should use retrofit with gson or jackson to call an API

There is nice explanation on it https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

with using retrofit , you no need to write asyncTask as well.

There is also another library volley to call an API.




回答4:


First you need to make it a valid JSON as it is invalid. You can rewrite JSON like this to make it valid and easily parsable.

{
"timestamp": 1471845537300,
"list": [{

    "id": "724206",
    "name": "HINGIS M. (SUI)",
    "points": "10790",
    "ranking": "1",
    "tour": "WTA-D",
    "lastUpdate": "2016-08-15"
}, {
    "id": "724207",
    "name": "MIRZA S. (IND)",
    "points": "10790",
    "ranking": "2",
    "tour": "WTA-D",
    "lastUpdate": "2016-08-15"
}, {
    "id": "724208",
    "name": "GARCIA C. (FRA)",
    "points": "6210",
    "ranking": "3",
    "tour": "WTA-D",
    "lastUpdate": "2016-08-15"
}, {
    "id": "724209",
    "name": "MLADENOVIC K. (FRA)",
    "points": "6045",
    "ranking": "4",
    "tour": "WTA-D",
    "lastUpdate": "2016-08-15"
}]}

You can parse JSON like this:

      //Create an object
    JSONObject obj = new JSONObject(responseString);

    obj.optLong("timestamp");

    //access your list array
    JSONArray list = obj.optJSONArray("list");

    for (int i = 0; i < list.length(); i++) {
        JSONObject post = list.optJSONObject(i);

        post.optString("id");
        post.optString("name");
        post.optString("points");
        post.optString("ranking");
        post.optString("tour");
        post.optString("lastUpdate");
        //do whatever you wants to do with these strings
    }


来源:https://stackoverflow.com/questions/39073000/how-to-get-all-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!