【发布时间】:2019-01-07 12:32:49
【问题描述】:
我有这个 json 代码
{
"status": "success",
"message": null,
"data": {
"19": {
"id": "19",
"created": "2019-01-07 13:26:06",
"createdby": 158,
"touched": "2019-01-07 13:26:06",
"touchedby": 158,
"start": "2019-01-07",
"end": "2019-01-08",
"scoperole": 3,
"scopecorp": 1,
"body": "<p>test</p>",
"language": "en"
},
"20": {
"id": "20",
"created": "2019-01-07 13:26:20",
"createdby": 158,
"touched": "2019-01-07 13:26:20",
"touchedby": 158,
"start": "2019-01-07",
"end": "2019-01-08",
"scoperole": 3,
"scopecorp": 1,
"body": "<p>test1</p>",
"language": "en"
}
},
"error": 0,
"line": 1515,
"debug": null
}
我想取 "body" (test,test1) 的值。
我怎样才能联系到他们?我留下了我试图查看它的代码,旁边有 //not working。有什么想法吗?另外 19,20 不一样,所以我无法通过仅输入变量名称来获取它们(例如 JSONObject dataObj =new JSONObject ("data");
这是我的代码
private void getJson() {
URL obj = null;
try {
obj = new URL("https://gekon.technologypark.cz/api/v1/notification/");
HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null)
sb.append(output);
JSONObject jsonObj = new JSONObject(sb.toString());
Log.d("test", "response" + jsonObj);
JSONObject dataObj =new JSONObject("data");
JSONObject dataObj19 =dataObj.getJSONObject("22");
String body1 =dataObj19.getString("body");
Log.d("test", "TEST ID" + body1);
pushNotification(notifText);
} catch (Exception e) {
e.printStackTrace();
Log.d("test", "PUSH CATCH" + e);
}
}
这是我的日志
**2019-01-07 14:17:42.778 19712-19712/ibm.gekon.vasileiosvlachakis.gekon D/alekos: 响应{"status":"success","message":null,"data":{"22":{"id":"22","created":"2019-01-07 14:11:55","createdby":158,"touched":"2019-01-07 14:11:55","touchedby":158,"start":"2019-01-07","end":"2019-01-08","scoperole":3,"scopecorp":1,"正文":"
test22
","language":"en"}},"error":0,"line":1515,"debug":null}2019-01-07 14:17:42.785 19712-19712/ibm.gekon.vasileiosvlachakis.gekon D/alekos:PUSH CATCHorg.json.JSONException:类型的值数据 java.lang.String 无法转换为 JSONObject**
【问题讨论】: