2

I'm using JsonReader from gson, for parsing JSON, my JSON parsing is fine for all value except this one :

  "tags": [
        "String1",
        "String2",
        "String3",
        "String4"
    ],

This is the code for parsing

 if(key.equals("tags")) {
 try {
        in.beginArray();

        List<String> tags = new ArrayList();

        while (in.hasNext()) {

            String value = in.nextString();
            tags.add(value);
        }

        item.setTags(tags);
        in.endArray();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

in is the JsonReader object, and item is the object where I store the result.

I don't understand why but when this line is done in.beginArray(); the in.hasNext() is sometimes true only once and sometimes not true at all. So I get only the first item of the list or nothing.

Is the format of the Array is correct ? If I use JSONLint for validate the all JSON he say that the JSON is correct

1
  • I think your json should be like : {"tags": [{ "String1", "String2", "String3", "String4" }] } Commented Mar 19, 2018 at 10:40

1 Answer 1

0

You can try this below code:

String jsonArrayString = obj.getJSONArray("tags").toString();
                        Gson googleJson = new Gson();
                        List tag = googleJson.fromJson(jsonArrayString, ArrayList.class);

                        System.out.println(tag);
                        item.setTags(tag);
Sign up to request clarification or add additional context in comments.

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.