0

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"
    ]
}

Comments

0

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));

1 Comment

Hey, thanks for your solution. I'm looking more for a solution that will directly convert the model to json. My actual checklist model has 20 something fields so your solution is not as suitable for my use case.

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.