json对象:
{"SessionID":"ae231c4b-6c69-4dec-8d52-be0786cdcdd8",
"RequestUniqueID":"34356566545677",
"ReportCode":"01",
"Condition":"{\"ReferenceNumber\":\"500\"}",
"MethodName":"TopupGetReport"}
我想解析这个对象;我该怎么做?
String response = "{\"SessionID\":\"ae231c4b-6c69-4dec-8d52-be0786cdcdd8\",\"RequestUniqueID\":\"34356566545677\",\"ReportCode\":\"01\",\"Condition\":{\"ReferenceNumber\":\"500\"},\"MethodName\":\"TopupGetReport\"}";
try {
JSONObject obj = new JSONObject(response);
String sessionId = obj.getString("SessionID");
String rqstUniqueId = obj.getString("RequestUniqueID");
String reportCode = obj.getString("ReportCode");
String methodName = obj.getString("MethodName");
JSONObject condition = obj.getJSONObject("Condition");
String referenceNumber = condition.getString("ReferenceNumber");
System.out.println(sessionId + ", " + rqstUniqueId + ", " + reportCode + ", " + methodName + ", " + referenceNumber);
} catch (JSONException e) {
e.printStackTrace();
}
产生:
ae231c4b-6c69-4dec-8d52-be0786cdcdd8, 34356566545677, 01, TopupGetReport, 500
注意:我认为定义数据的括号中的几个额外引号Condition
只是一个错误,可能是从一些测试代码中复制到您的问题中的。我删除了它们(请参阅我的response
字符串)。