0

I'm new to JSON. I'm trying to assign value (in key value pairs) as array of strings using GSON. The JSON should look like as below:

{ "name": "path", "value": [ "/my-path" ,"/my-path2","/newpath"] }

How can I achieve this?

Thanks.

1
  • 1. Change gson to JSON in title 2. here Commented Mar 7, 2017 at 8:47

1 Answer 1

1

Even thoe I will hardly recommend you to use POJOS, gson is flexible enough to allow you to do what you want:

    JsonObject jo = new JsonObject();
    jo.addProperty("name", "path");

    JsonArray jsonArray = new JsonArray();
    jsonArray.add("my-path");
    jsonArray.add("my-path2");
    jsonArray.add("my-new-path");
    jo.add("value", jsonArray);

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

1 Comment

Quite simple, and shows how Gson lacks nice fluent interface methods for the add methods family.

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.