无法从下面的json中获取customer.id字段的信息:{"date":"2019-10-29T21:34:07.391Z","customer":{"id":"9999999999999999999"}}
我尝试将客户映射为JSONObject并获取关键字" id“,但不起作用我尝试将客户映射为JSONObject,将id映射为JSONObject,但不起作用
JSONObject json = new JSONObject("{\"customer\":{\"id\":\"9999999999999999999\"},\"date\":\"2019-10-29T21:34:07.391Z\"}");
JSONObject customer = new JSONObject(json.get("customer"));
// Show full string
System.out.println(json);
// Date returned without problems
System.out.println("date: "+json.get("date"));
// Customer object returend without problems
System.out.println("customerObject"+json.get("customer"));
// Trying to extract info - both failed
try{
System.out.println("customerId: "+customer.getString("id"));
} catch (JSONException e){
System.out.println("getString(id) failed: "+e.toString());
}
try{
JSONObject id = new JSONObject(customer.get("id"));
} catch (JSONException e){
System.out.println("customer.get(id) failed: "+e.toString());
}急诊室:
{"date":"2019-10-29T21:34:07.391Z","customer":{"id":"9999999999999999999"}}
date: 2019-10-29T21:34:07.391Z
customerObject{"id":"9999999999999999999"}
customerId: 9999999999999999999AR:
{"date":"2019-10-29T21:34:07.391Z","customer":{"id":"9999999999999999999"}}
date: 2019-10-29T21:34:07.391Z
customerObject{"id":"9999999999999999999"}
getString(id) failed: org.json.JSONException: JSONObject["id"] not found.
customer.get(id) failed: org.json.JSONException: JSONObject["id"] not found.发布于 2019-08-18 21:57:21
试试这个:在创建JSONObject customer时添加toString()
JSONObject json = new JSONObject("{\"customer\":{\"id\":\"9999999999999999999\"},\"date\":\"2019-10-29T21:34:07.391Z\"}");
JSONObject customer = new JSONObject(json.get("customer").toString());
// Show full string
System.out.println(json);
// Date returned without problems
System.out.println("date: "+json.get("date"));
// Customer object returend without problems
System.out.println("customerObject"+json.get("customer"));
// Trying to extract info - both failed
try{
System.out.println("customerId: "+customer.getString("id"));
} catch (JSONException e){
System.out.println("getString(id) failed: "+e.toString());
}
try{
JSONObject id = new JSONObject(customer.get("id"));
} catch (JSONException e){
System.out.println("customer.get(id) failed: "+e.toString());
}https://stackoverflow.com/questions/57545011
复制相似问题