2

Json Object:

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

I want to parse this object; how do I do so?

Nate
  • 31,017
  • 13
  • 83
  • 207
Nancy Jain
  • 204
  • 1
  • 7

1 Answers1

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();
  }

which yields:

ae231c4b-6c69-4dec-8d52-be0786cdcdd8, 34356566545677, 01, TopupGetReport, 500

Note: I assume that a couple extra quotes around the brackets defining the Condition data were just a mistake, probably copied from some test code, into your question. I removed them (see my response string).

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Hello it is json object which will cum from server side and then i have to post it using hash table so how to do it???? – Nancy Jain Aug 15 '13 at 10:17
  • @NancyJain, If you are getting the JSON from a server, then the variable named `response` that I show above would be replaced by a `String` of content that the server sent you. I'm not sure I understand your comment *"i have to post it using hash table"*. Do you mean that you need to put the JSON values into a hashtable object on the device side, after you've parsed the server response? – Nate Aug 15 '13 at 19:05