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?

1
  • One would use a JSON parser. You have your choice of about 20 different ones, listed at json.org. Commented May 22, 2015 at 3:20

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I use Gson library from Google

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

Comments

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.

1 Comment

If you think this answer your question, please check the green sign below the arrows on the left-side of the question.
1

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

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.