我想将JSON结果放在textviews中,但由于多个数组,我只能获得datetime,location和status对象的一个键/值。 json对象是:
{
"signature":"testSignature",
"deliverydate":"2015-08-06 15:07:00",
"datetime":{
"0":1438848420,
"1":1438841820,
"2":1438838760,
},
"location":{
"0":"PA",
"1":"PA",
"2":"PA",
},
"status":{
"0":"packed",
"1":"On the go",
"2":"delivered",
},
"pickupdate":2015-08-04 07:55:00
}
这是我的java代码:
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("NO", NUMBER_TO_POST));
JSONObject json = jsonParser.makeHttpRequest(URL_TO_POST, "POST", params);
success = json.getString(TAG_SIGNATURE);
if (success != null) {
SIGNATURE = json.getString(TAG_SIGNATURE);
DELIVERY_DATE = json.getString(TAG_DELIVERY_DATE);
JSONObject DT = json.getJSONObject(TAG_DATETIME);
DATETIME = DT.getString("0");
JSONObject LOC = json.getJSONObject(TAG_LOCATION);
LOCATION = LOC.getString("0");
JSONObject STAT = json.getJSONObject(TAG_STATUS);
STATUS = STAT.getString("0");
PICKUP_DATE = json.getString(TAG_PICKUP_DATE);
}else{
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
任何人都可以帮我解决这个问题吗?感谢
答案 0 :(得分:1)
您应该使用GSON库来解析JSON。
为了更有帮助,以下是保存JSON值的类如下所示:
class MyClassForGsonToHoldParseJSON {
String signature;
String deliverydate;
Map<String, long> datetime;
Map<String, String> location;
Map<String, String> status;
String pickupdate;
}
然后使用这样的东西将带有JSON数据的变量json
转换为对象:
Gson gson = new Gson();
MyClassForGsonToHoldParseJSON f = gson.fromJson(json, MyClassForGsonToHoldParseJSON.class);
答案 1 :(得分:0)
您的JSON格式错误:
{
"signature": "testSignature",
"deliverydate": "2015-08-06 15:07:00",
"datetime": {
"0": 1438848420,
"1": 1438841820,
"2": 1438838760
},
"location": {
"0": "PA",
"1": "PA",
"2": "PA"
},
"status": {
"0": "packed",
"1": "On the go",
"2": "delivered"
},
"pickupdate": " 2015-08-04 07:55:00"
}