Thursday 3 October 2013

Ways to create JSON object


1.
String response = "[
    {"result":
    {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}
    ]"



JSONArray jsonArrayResponse = new JSONArray(response);

//display
"[
    {"result":
    {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}
    ]"





2.
JSON jsonObj = jsonArrayResponse.getJSONObject(0).getJSONObject("result");




//display {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}

3.
JSONObject x = (JSONObject) jsonObj.get("3");
//display
{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"}
               
JSONObject cvb = new JSONObject(jsonObj.get("3").toString());

{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"}

String hold1 =    cvb.get("Hold").toString();

// display 0
           
String hold =    x.get("Hold").toString();

//display 0

System.out.println(x);



One additional method


4. jsonObj = JSONFactoryUtil.createJSONObject(result);