-2

I am new to JSON and I have been referring to some youtube videos for the question given in the link question on how to handle the error but I still don't understand why I am getting the same error.

The error log are as following:

2021-01-12 10:30:10.327 30666-315/... W/System.err: org.json.JSONException: Value [{"lastUpdateTs":1610342055312,"key":"capacity","value":20},{"lastUpdateTs":1610342069003,"key":"free","value":4},{"lastUpdateTs":1610342055324,"key":"capacity","value":20},{"lastUpdateTs":1610342070857,"key":"free","value":4}] of type org.json.JSONArray cannot be converted to JSONObject
2021-01-12 10:30:10.327 30666-315/... W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:168)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:181)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at ...PremisesListTaskLoader.loadInBackground(PremisesListTaskLoader.java:118)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at ....PremisesListTaskLoader.loadInBackground(PremisesListTaskLoader.java:20)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at androidx.loader.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:307)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:60)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at androidx.loader.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:48)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at androidx.loader.content.ModernAsyncTask$2.call(ModernAsyncTask.java:141)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-01-12 10:30:10.328 30666-315/... W/System.err:     at java.lang.Thread.run(Thread.java:919)

here's my code:

try {      
      if (code2 / 100 == 2) {
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(connection2.getInputStream()));
            StringBuilder sb2 = new StringBuilder();
            String input2;
            while ((input2 = reader2.readLine()) != null) {
                  sb2.append(input2);
            }
            reader2.close();

            JSONObject response2 = new JSONObject(sb2.toString());  // <- The line mentioned   at ...PremisesListTaskLoader.loadInBackground(PremisesListTaskLoader.java:118)
            JSONArray data2 = response2.getJSONArray("data");
            int length2 = data2.length();

            for (int i2 = 0; i2 < length2; i2++) {
                  JSONObject object2 = data2.getJSONObject(i2);
                  String deviceType = object2.getString("key");
                  String deviceId = object2.getString("value");
            }
    }
      connection2.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }

The data that I want to retrieve (which are stored in input2) are as following:

[
  {
    "lastUpdateTs": 1604651123669,
    "key": "capacity",
    "value": 20
  },
  {
    "lastUpdateTs": 1606467758647,
    "key": "free",
    "value": 12
  }
]
1
  • 1
    Please learn JSON. [ ] is a JSON array, so why do you believe that parsing it as a JSON object would work? Since the root is an array, change new JSONObject(sb2.toString()) to new JSONArray(sb2.toString()). Commented Jan 12, 2021 at 3:48

1 Answer 1

1
[
  {
    "lastUpdateTs": 1604651123669,
    "key": "capacity",
    "value": 20
  },
  {
    "lastUpdateTs": 1606467758647,
    "key": "free",
    "value": 12
  }
]

This is JSONArray format cause could not convert by JSONObject response2 = new JSONObject(sb2.toString());

Replace:

JSONObject response2 = new JSONObject(sb2.toString());  
JSONArray data2 = response2.getJSONArray("data");
int length2 = data2.length();

for (int i2 = 0; i2 < length2; i2++) {
   JSONObject object2 = data2.getJSONObject(i2);
   String deviceType = object2.getString("key");
   String deviceId = object2.getString("value");
}

With:

JSONArray jsonArray = new JSONArray(sb2.toString());  
for (int i2 = 0; i2 < jsonArray.length(); i2++) {
   JSONObject object2 = jsonArray.getJSONObject(i2);
   String deviceType = object2.getString("key");
   String deviceId = object2.getInt("value");
}
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.