0

I'm making HTTP requests to a server and I my response comes back as a string in the following format:

[
  {
    "key1": "val1"
  },
  {
    "key2": "val2"
  }
]

So it is given back as a string list and each 'element' in the string is a JSON string.

What is the most effective way to retrieve each json?

I'd strip the square brackets on the first and last and then do a string.split({regex}) to get a string array but I'm not sure if that's the best way?

But the regex might get quite convoluted as I can't just split on "," as some key value pairings could have a list as values which will also include "," so determining when the first json actually starts might be tricky.

Are there any libraries that can help?

Thanks

5

1 Answer 1

1

Found the answer,

JsonArray root = new JsonParser().parse(response).getAsJsonArray();

Cheers

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.