0

I have a string like that:

String s = "{"Player":{"id":"1","name":"PM001"}},{"Player":{"id":"2","name":"PM002"}}"

I want to omit the "," so the result should be :

{"Player":{"id":"1","name":"PM001"}}

{"Player":{"id":"2","name":"PM002"}}
7
  • 2
    And what you have tried so far?? Commented Sep 1, 2014 at 10:10
  • Where does this string come from? Commented Sep 1, 2014 at 10:12
  • 2
    It looks like a json object. Have you already tried the json-Api for Java to extract the elements? Commented Sep 1, 2014 at 10:12
  • yes i tried to use json api, i want to retreive all the id, but just i can retreive only the first id Commented Sep 1, 2014 at 10:16
  • wrap it in a pair of square brackets and parse it as json. Commented Sep 1, 2014 at 10:17

2 Answers 2

3
JSONArray newArray = new JSONArray(s);
newArray.get(0);
newArray.get(1);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for ryour reply, but an error occured "Cannot instantiate the type JSONArray"
What is the return Object of s ? String or what?
If it is not work set this String to JSONObject and call getJSONArray
1

I would just split on a comma surrounded by reversed curly brackets:

String[] parts = s.split("(?<=}),(?=\\{)");

This uses look arounds to assert the presence of, but not consume, the curly brackets.

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.