Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleJson: String to JSONArray

I get the following JSON:

[
  {
    "user_id": "someValue"
  }
]

It's saved inside a String.

I would like to convert it to a JSONObject which fails (as the constructor assumes a JSON to start with {). As this doesn't seem to be possible I'd like to convert it to a JSONArray. How can I do that with SimpleJson?

like image 735
mosquito87 Avatar asked Feb 25 '16 09:02

mosquito87


1 Answers

JSONParser parser = new JSONParser();
JSONArray array = (JSONArray)parser.parse("[{\"user_id\": 1}]");
System.out.println(((JSONObject)array.get(0)).get("user_id"));

You need to cast to a JSONArray as that is what the string contains.

like image 141
Michael Lloyd Lee mlk Avatar answered Sep 20 '22 13:09

Michael Lloyd Lee mlk