2

I have following json which i need to convert into list of java objects.

{
  "model":[

    {
        "syscall_1":"execve",
        "syscall_2":"brk"
    },
    {
        "syscall_1":"brk",
        "syscall_2":"access"
    },
    {
        "syscall_1":"access",
        "syscall_2":"mmap"
    },
    {
        "syscall_1":"mmap",
        "syscall_2":"access"
    }
]
}

I am using gson and its TypeToken>(){}.getType() API ,however I am bit confused about how my objects should look corresponding to input json.

How can I use TypeToken in this scenario?

2 Answers 2

1

Another option (not using the type token, but still achieves what you want) would be to parse the entire json object, then access the model array like so:

import com.google.gson.Gson;

import java.util.List;


public class TestMe {

    public static void main(String[] args) {

        String jsonSt2 = "{\"model\":[{\"syscall_1\":\"execve\",\"syscall_2\":\"brk\"},{\"syscall_1\":\"brk\",\"syscall_2\":\"access\"},{\"syscall_1\":\"access\",\"syscall_2\":\"mmap\"},{\"syscall_1\":\"mmap\",\"syscall_2\":\"access\"}]}";
        System.out.println("your json: " + jsonSt2);

        ModelObject object = new Gson().fromJson(jsonSt2, ModelObject.class);

        System.out.println("Created Model object, array size is " + object.model.size());

        for (ModelItem mi : object.model) {
            System.out.println(mi.syscall_1 + " " + mi.syscall_2);
        }
    }
}

class ModelObject {
    List<ModelItem> model;
}

class ModelItem {
    String syscall_1;
    String syscall_2;
}

Output

Created Model object, array size is 4
execve brk
brk access
access mmap
mmap access
mmap access
Sign up to request clarification or add additional context in comments.

Comments

0

If you can use org.json to parse and construct list objects, you can try this.

String jsonSt2 = "{\"model\":[{\"syscall_1\":\"execve\",\"syscall_2\":\"brk\"},{\"syscall_1\":\"brk\",\"syscall_2\":\"access\"},{\"syscall_1\":\"access\",\"syscall_2\":\"mmap\"},{\"syscall_1\":\"mmap\",\"syscall_2\":\"access\"}]}";
List<Model> models = new ArrayList<>();
JSONObject jsonModelObject = new org.json.JSONObject(jsonSt2);
Object modelObject = jsonModelObject.get("model");
if (modelObject instanceof JSONArray) {
JSONArray itemsArray =(JSONArray) modelObject;
for (int index = 0; index < itemsArray.length(); index++) {
    Model model = new Model();
    JSONObject modelItereative = (JSONObject) itemsArray.get(index);
    model.setSyscall_1(modelItereative.getString("syscall_1"));
    model.setSyscall_2(modelItereative.getString("syscall_1"));
    models.add(model);
}
}else if(modelObject instanceof JSONObject){
Model model = new Model();
JSONObject modelItereative = (JSONObject) modelObject;
model.setSyscall_1(modelItereative.getString("syscall_1"));
model.setSyscall_2(modelItereative.getString("syscall_1"));
models.add(model);
}
for(Model d22:models){
    System.out.println(d22.getSyscall_1() + " " + d22.getSyscall_2());
}

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.