我找到了很多答案,但它表示字符串,我想要所有jsonobjects LINK1 LINK2
这是我的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"
}
}
我添加了一个屏幕截图,所以你可以理解
我试过了。
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
答案 0 :(得分:1)
首先你的json是不正确的 因为你可以在jsonlint上验证它
这是我在验证你的json时面临的问题
Error: Parse error on line 1:
{ timestamp: 147184553
----^
Expecting 'STRING', '}', got 'undefined'
答案 1 :(得分:1)
如果您的 //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
}
是这样的,
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();
}
答案 2 :(得分:0)
您应该使用改装与 gson 或杰克逊来调用API
有很好的解释https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
使用改造,你也不需要编写asyncTask。
还有另一个图书馆凌空调用API。
答案 3 :(得分:0)
首先,您需要使其成为有效的JSON,因为它无效。您可以像这样重写JSON,使其有效且易于解析。
delete base
您可以像这样解析JSON:
{
"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"
}]}