SL07
SL07

Reputation: 103

Convert Java Object with String Array as a Field to JSONObject

I have a data model with that is a String[]. When I try to convert the model into JSONObject using the following code:

public class CheckList {
    private String id = "123";
    private String Q1 = "This is Question 1";
    private String[] Q2 = ["Part 1", Part 2"];

    public CheckList (String, id, String Q1, String[] Q2){
       ...
    }
}

CheckList checklist = new Checklist("123", "This is Question 1", ["Part 1", "Part 2"]

JSONObject test = new JSONObject(checklist);

the String[] is not being converted properly. With the code above, I want a JSONObject look like this:

{
  id: 123,
  Q1: This is Question 1,
  Q2: [Part 1, Part 2]
}

but I'm getting JSONObject like this:

{
  id: 123,
  Q1: This is Question 1,
  Q2: [{"bytes":[{},{},{},{}],"empty":false},{"bytes":[{},{},{},{}],"empty":false}]
}

Is there any way to fix this issue? Thanks in advance.

Upvotes: 0

Views: 1014

Answers (3)

Darshan Mehta
Darshan Mehta

Reputation: 30849

You might need to use JsonArray inside CheckList class to deserialize the array. However, if your implementation permits, you can use Jackson to convert the object inso json, it's easy to use and does not require bits like JsonArray to convert the object. Below is an example:

public class CheckList {
    private String id = "123";
    private String Q1 = "This is Question 1";
    private String[] Q2;

    public CheckList (String id, String Q1, String[] Q2){
       this.id = id;
       this.Q1 = Q1;
       this.Q2 = Q2;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getQ1() {
        return Q1;
    }

    public void setQ1(String q1) {
        Q1 = q1;
    }

    public String[] getQ2() {
        return Q2;
    }

    public void setQ2(String[] q2) {
        Q2 = q2;
    }

    public static void main(String[] args) throws Exception{
        CheckList checklist = new CheckList("123", "This is Question 1", new String[]{"Part 1", "Part 2"});
        ObjectMapper objectMaapper = new ObjectMapper();
        System.out.println(objectMaapper.writeValueAsString(checklist));

    }
}

Here's maven central URL for Jackson and here's documentation.

Upvotes: 1

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3759

You can use Gson it's so efficient:

class CheckList {
    private String id = "123";
    private String Q1 = "This is Question 1";
    private String[] Q2 = {"Part 1", "Part 2"};
}


final String jsonObject = new Gson().toJson(new CheckList());

System.out.print(jsonObject);

Output :

{
    "id": "123",
    "Q1": "This is Question 1",
    "Q2": [
        "Part 1",
        "Part 2"
    ]
}

Upvotes: 0

Patricia
Patricia

Reputation: 2865

Put the entries step by step into the JSONObject and convert the array first to an ArrayList<String>.

ArrayList<String> list = new ArrayList<String>();
list.add("Part 1");
list.add("Part 2");

JSONObject test = new JSONObject();
test.put("id", 123);
test.put("Q1","This is Question 1");
test.put("Q2", new JSONArray(list));

Upvotes: 0

Related Questions