0

How to parse HTTP response ton JSON object ?

I changed my json code to:

{
    "data": [{
        "id": 1,
        "sensor1": 76,
        "sensor2": 75
    }, {
        "id": 2,
        "sensor1": 76,
        "sensor2": 206
    }]
}

And my code is now:

JSONObject json = new JSONObject(mJson);
try {
data = json.getJSONArray("data");
for (int i = 0; i < json.length(); i++) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    JSONObject obj = (JSONObject) data.get(i);

    map.put(TAG_SENSOR_1, obj.getInt("sensor1"));
    map.put(TAG_SENSOR_2, obj.getInt("sensor2"));

    dataList.add(data.getInt("id") - 1, map);
}
} catch (JSONException e) {
e.printStackTrace();
}

But I have the same problem

8
  • What library are you using to parse the JSON? The value that you're passing to it is not valid JSON data, hence the error. Commented Jan 8, 2014 at 2:07
  • It would appear that this array of arrays is stored as a string. It is likely enclosed by quotes in the raw json data. Commented Jan 8, 2014 at 2:20
  • What code you are trying to parse this JSON? Commented Jan 8, 2014 at 2:42
  • your string is not in Json format Commented Jan 8, 2014 at 4:39
  • I use: JSONObject json = new JSONObject(my_json_string); Commented Jan 8, 2014 at 7:36

1 Answer 1

1

edit your string to this format

{content:[{
              first:1,
              second:76,
              third:75
          },
          {
              first:2,
              second:76,
              third:75
          },
          {
              first:3,
              second:206,
              third:75
          },
          {
              first:4,
              second:6,
              third:175
          },
          {
              first:5,
              second:176,
              third:75
          }]
}

then use JSONObject and JSONArray to convert string

JSONArray array = new JSONObject(string).getJSONArray("content");
for(int i=0;i<array.size();i++){
    JSONObject obj = array.get(i);
    Log.i(TAG,"obj.getInt = "+obj.getInt("first"));
    Log.i(TAG,"obj.getInt = "+obj.getInt("second"));
    Log.i(TAG,"obj.getInt = "+obj.getInt("third"));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.