Nilesh
Nilesh

Reputation: 281

Converting a String to array of JSON objects and parsing those

I have a string in the format like this

[{name:"abc",test:"123",jack:{tret:"abc",cold:"yes"},killer:"yes"},{name:"pqr",test:"456",jack:{tret:"hg",cold:"No"},killer:"No"}]

I know that this is an array of JSON objects.How do I convert this so?

As of now i am just using split and removing all the commas and other things from the string and storing in an array.Is there any better way to do so?

Upvotes: 2

Views: 959

Answers (4)

Batman22
Batman22

Reputation: 366

You can use Jackson library and further you can go through this for parsing http://www.mkyong.com/java/jackson-tree-model-example/

Upvotes: 1

EduardoFernandes
EduardoFernandes

Reputation: 3439

Use the https://code.google.com/p/json-simple/ library for handling JSON.

Here is a sample code:

JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("width"));

Please refer to this stackoverflow answer for more answers in this matter.

Upvotes: 2

PhuLuong
PhuLuong

Reputation: 537

I use Gson library from Google

JsonElement parse = (new JsonParser()).parse(jsonString);

Upvotes: 2

blazetopher
blazetopher

Reputation: 1050

Using a JSON library is definitely the way to go, but I might suggest one that's being actively developed such as jackson or gson. Both have extensive documentation and examples.

Upvotes: 2

Related Questions