2

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?

Nilesh
  • 281
  • 1
  • 11

4 Answers4

2

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.

Community
  • 1
  • 1
EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12
2

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.

blazetopher
  • 1,050
  • 9
  • 13
2

I use Gson library from Google

JsonElement parse = (new JsonParser()).parse(jsonString);
PhuLuong
  • 527
  • 4
  • 11
1

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

Batman22
  • 376
  • 4
  • 25