2

I have this String from a JSON value:

["1","5","10","15","20"]

I need to get a string array from this string. How do I do that?

6
  • 3
    try do yourstring.split(",") which gives u string array? Commented Apr 7, 2013 at 3:18
  • 2
    I would imagine that removing the first and last characters and then using split() would do it if you're looking for an array consisting of "1", "5", "10", "15", and "20". Commented Apr 7, 2013 at 3:18
  • Remove the first two characters, the last two characters and split() on "\",\"" Commented Apr 7, 2013 at 3:20
  • Why is your JavaScript sending a string instead of an array? Can't JSON handle arrays? Commented Apr 7, 2013 at 3:24
  • It's Java, not JavaScript Commented Apr 7, 2013 at 3:26

1 Answer 1

7

You can use gson library like this:

Gson gson = new Gson(); // create gson instance

String[] strArr = gson.fromJson(jsonString, String[].class);
Sign up to request clarification or add additional context in comments.

1 Comment

If you're reading JSON values, you should use a JSON-parsing library. Don't try to parse them yourself because you won't be able to handle every possible invalid input (with the proper exception) unless you put a lot of extra (and unnecessary) effort into it.

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.