-2

How to parse below json response

{
  "device": "thane1",
  "sensor": "arduino",
  "data": {
    "1467871629": 303,
    "1467871661": 303,
    "1467871693": 303,
    "1467871720": 303,
    "1467871755": 303,
    "1467871785": 303,
    "1467871807": 303,
    "1467886625": 326,
    "1467886659": 327,
    "1467886692": 326,
    "1467886725": 326,
    "1467886858": 326
  }
}

Code used:

private class getChartData extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... params) {
            return getData();
        }

        protected String getData() {
            String decodedString = "";
            String returnMsg = "";
            String request = "http://52.77.220.93:4000/getLast?device=thane1&sensor=arduino&lim=300";
            URL url;
            HttpURLConnection connection = null;
            try {
                url = new URL(request);
                connection = (HttpURLConnection) url.openConnection();
                connection.addRequestProperty("Content-Type", "application/json");
                connection.setRequestMethod("GET");
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//parsing here
}
Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63
smsubham
  • 21
  • 9

1 Answers1

0

I think this following tutorials are helpful for you:

How to parse JSON in Android

http://www.tutorialspoint.com/android/android_json_parser.htm http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Read it carefully to know how to do it :)

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
  • thanks for trying to answer... already gone through those link earlier.... there the if part is fixed but as you can see it's varies in response I have received... like Integer.parseInt(jsonObject.optString("id").toString()); id passed as parameters to optString() varies ... please correct me if am wrong in interpreting it – smsubham Jul 30 '16 at 22:08
  • Use this link instead - it's part of the core API: https://developer.android.com/reference/org/json/JSONObject.html – jesses.co.tt Jul 30 '16 at 23:21