我找到了很多答案,但它表示字符串,我想要所有的jsonobjects LINK1 LINK2
这是我的儿子
{
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"
}
}我加了一个截图,这样你就可以理解

我试过这个。
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发布于 2016-08-22 07:51:51
如果你的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"
}
}注意到:我不确定type是在list对象的外部还是内部。我认为它在list对象中(通常不应该是这样的)。
下面是一个关于如何解析这个的快速而肮脏的解决方案,您可以使用instanceof关键字来检查数据是String还是JSONObject。问题是,如果您得到的东西不是String或JSONObject,它就不会被解析。
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();
}产出,
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发布于 2016-08-22 06:59:06
首先,您的json是错误的,因为您可以在jsonlint上验证它。
http://www.jsonlint.com/
这就是我在验证您的json时所面临的问题。
Error: Parse error on line 1:
{ timestamp: 147184553
----^
Expecting 'STRING', '}', got 'undefined'发布于 2016-08-22 07:09:22
您应该使用retrofit和gson或jackson来调用API。
上面有很好的解释,https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
通过使用改进,您也不需要编写asyncTask。
还有另一个调用API的库volley。
https://stackoverflow.com/questions/39073000
复制相似问题