我想从Server解析一个数组,但我无法获取该数组 这是成功获得的jsonString:
{
"status":"OK",
"message":"this is your start and end coordinates",
"data":"{\"start\":[\"35.778763\",\"51.427360\"],\"end\":[\"35.768779, 51.415002\"]}"
}
我想要数据arraylist的双值:
//try/catch
Log.d(TAG, "Passes here");
JSONObject jObject = new JSONObject(jsonString);
JSONArray jData = jObject.getJSONArray("data");
Log.d(TAG, "Not PAASING HERE !!! ");
JSONArray jArrayStart = (JSONArray) jData.get(0);
JSONArray jArrayEnd = (JSONArray) jData.get(1);
latitudeStart = (Double) jArrayStart.get(0);
longtitudeStart = (Double) jArrayEnd.get(1);
latitudeEnd = (Double) jArrayEnd.get(0);
longtitudeEnd = (Double) jArrayEnd.get(1);
答案 0 :(得分:2)
您要解析的内容是string
。
{
"status": "OK",
"message": "this is your start and end coordinates",
"data": "{\"start\":[\"35.778763\",\"51.427360\"],\"end\":[\"35.768779, 51.415002\"]}"
}
所以它的工作原理如下:
//first, retrieve the data from the response JSON Object from the server
JSONObject response = new JSONObject(jsonString);
String status = response.getString("status");
String message = response.getString("message");
//Note this: "data" is a string as well, but we'll have to parse that later.
String data = response.getString("data");
//get the doubles from the arrays from the "data" component.
JSONObject dataObject = new JSONObject(data);
JSONArray start = dataObject.getJSONArray("start");
JSONArray end = dataObject.getJSONArray("end");
for (int i = 0; i < start.length(); i++) {
String value = start.getString(i);
//do something with the start String (parse to double first)
}
for (int i = 0; i < end.length(); i++) {
String value = end.getString(i);
//do something with end String (parse to double first)
}
因此,数据实际上是String
,但代表JSONObject
(您必须解析),而后者又包含两个JSONArrays
。
如果data
是JSONObject
而不是String
,则JSON看起来像这样:
{
"status": "OK",
"message": "this is your start and end coordinates",
"data": {
"start": [
"35.778763",
"51.427360"
],
"end": [
"35.768779", //note that in your example, this quote is missing (first quote on next line too)
"51.415002"
]
}
}
答案 1 :(得分:1)
data
的值不是JSONArray
JSONObject
JSONObject
将被{}
JSONArray
将被[]
答案 2 :(得分:0)
数据不是数组,它是一个json对象,因此您无法按照自己的方式访问它。 如果你想从json对象“data”获取start数组,那么请在下面使用 jObject.optJSONObject( “数据”)optJSONArray( “开始”);
同样的东西可以用来检索“end”json数组。
然后使用optJSONObject()和/或optString()API从json数组中检索所需的值。