-2

I want to get the value of "result" from the below JSON response and store it locally.Here's the code:

private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        //JSONArray contacts;
                        contacts = jsonObj.getJSONArray("response");
                        Log.d("Response: ", "> " + contacts);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }
                return null;
        }
    }

My Response :

{"response":

        [{
            "name":"ajay",
            "class":"7",
        },

        {
            "rank":1
        }],


        "date":

        {
            "startdate":2/12/2012,
        },
            "result":"pass"
        }
onexf
  • 3,674
  • 3
  • 22
  • 36
Ben
  • 73
  • 2
  • 12

3 Answers3

6

You need to create a JSON Object from json String, you get and then retrieve its data:

JSONObject json= new JSONObject(responseString);  //your response
try {
    String result = json.getString("result");    //result is key for which you need to retrieve data
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
4

Please provide correct and full JSON response. So I can show you the way to parse the JSON :

String jsonStr;  // hold your JSON response in String
try {
    JSONObject jsonObj = new JSONObject(jsonStr);

    // If you have array
    JSONArray resultArray = jsonObj.getJSONArray("response"); // Here you will get the Array

    // Iterate the loop
    for (int i = 0; i < resultArray.length(); i++) {
        // get value with the NODE key
        JSONObject obj = resultArray.getJSONObject(i);
        String name =  obj.getString("name");
    }

    // If you have object
    String result = json.getString("result");

} catch (Exception e) {
    e.printStackTrace();
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

Try like this...

 @Override
    protected Void doInBackground(Void... arg0) {
    // Creating service handler class instance
    ServiceHandler sh = new ServiceHandler();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

     if (jsonStr != null) {
      try {
      JSONObject json= new JSONObject(jsonStr);  //your json response
      String result = json.getString("result");    //result data
      } catch (JSONException e) {
        e.printStackTrace();
      }
     }
    }
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44