如何在J2ME中解析嵌套的json对象

时间:2013-08-15 08:01:53

标签: json java-me

Json对象:

{"SessionID":"ae231c4b-6c69-4dec-8d52-be0786cdcdd8",
"RequestUniqueID":"34356566545677",
"ReportCode":"01",
"Condition":"{\"ReferenceNumber\":\"500\"}",
"MethodName":"TopupGetReport"}

我想解析这个对象;我该怎么做?

1 个答案:

答案 0 :(得分:1)

  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字符串)。