1

I have a mind-boggling problem. I have the following String:

[{"Time":0,"HR":0,"Steps":0,"Battery":0,"Screen wake":0,"Do-not-disturb":0,"Charger":0,"Notification":0},{"Time":0,"HR":0,"Steps":0,"Battery":0,"Screen wake":0,"Do-not-disturb":0,"Charger":0,"Notification":0}]

I am trying to convert this to JSONArray in order to add more elements like this:

        jsonArrayData = gson.fromJson(string, JSONArray.class);

This is throwing error

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I can get my head around what I do wrong ...

1
  • If you are using Gson, why are you trying to parse it into JSONArray from the org.json parser? Pick one JSON parser and use it. Create a Java class that models your HR data and have Gson parse your JSON into an ArrayList of that type. Commented Aug 1, 2021 at 22:17

1 Answer 1

1

Well, it was as simple as

jsonArrayData = new JSONArray(string);

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.