5

I have JSON String, received from HTTP request:

     [  
       {  
      "id":15,
      "title":"1",
      "description":"desc",
      "user_id":152
     },
    {  
      "id":18,
      "title":"2",
      "description":"desc",
      "user_id":152
    },
    {  
      "id":19,
      "title":"tab3",
      "description":"zadanka",
      "user_id":152
   }
]

How to convert it into an ArrayList of Objects?

4 Answers 4

6

Using Gson

Gson gson = new Gson();
        ArrayList<Object> listFromGson = gson.fromJson("json string",
               new TypeToken<ArrayList<Object>>() {}.getType());

Using Jackson

    ObjectMapper mapper = new ObjectMapper();
    ArrayList<Object> listFromJackson = mapper.readValue("json string",
           new TypeReference<ArrayList<Object>>(){});

If you could define a pojo as

public class Example {

    private Integer id;
    private String title;
    private String description;
    private Integer userId;
    // setters / getters
}

Then

ArrayList<Example> listFromGson = gson.fromJson("json string",
               new TypeToken<ArrayList<Example>>() {}.getType());

ArrayList<Example> listFromJackson = mapper.readValue("json string",
               new TypeReference<ArrayList<Example>>(){});

Also, you should prefer using List instead of ArrayList.

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

Comments

5

You need to declare a pojo

class Data{
  String id;
  String title;
  String description;
  String userId;
  //Generate setter an getter
}

The iterate over json like following:

JSONArray jsonArr = new JSONArray("[your JSON Stirng]");
    List<Data> dataList = new ArrayList<Data>();
    for (int i = 0; i < jsonArr.length(); i++) {
        JSONObject jsonObj = jsonArr.getJSONObject(i);
        Data data = new Data();
        data.setId(jsonObj.getString("id"));
        data.setTitle(jsonObj.getString("title"));
        data.setDescription(jsonObj.getString("description"));
        data.setUserId(jsonObj.getString("user_id"));
        dataList.add(data);
    }

You also need json jar. You can download from here

2 Comments

Thank you. This is what I needed.
Rather than downloading from the link the poster provided, use Maven and a legit source: mvnrepository.com/artifact/org.json/json
1

If you are using RestApi then use the annotation @RequestBody with your pojo class.

@RequestMapping(value="/your api name", method=RequestMethod.POST)
public ResponseData createUser(@RequestBody MyPojo myPojo){
System.out.println("Creating User "+myPojo.toString());

//Here you will able to access your request data from myPojo object
}

Make your pojo class:

public class MyPojo
{
private Data[] data;

public Data[] getData ()
{
    return data;
}

public void setData (Data[] data)
{
    this.data = data;
}

@Override
public String toString()
{
    return "ClassPojo [data = "+data+"]";
}
}

public class Data
{
private String id;

private String title;

private String description;

private String user_id;

public String getId ()
{
    return id;
}

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

public String getTitle ()
{
    return title;
}

public void setTitle (String title)
{
    this.title = title;
}

public String getDescription ()
{
    return description;
}

public void setDescription (String description)
{
    this.description = description;
}

public String getUser_id ()
{
    return user_id;
}

public void setUser_id (String user_id)
{
    this.user_id = user_id;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", title = "+title+", description = "+description+", user_id = "+user_id+"]";
}
}

Comments

1

In addition to @Sudhir, I would recommend to use Gson

Gson gson = new GsonBuilder().create();
 Data p = gson.fromJson(jsonString, Data.class);
// Or to array.
 Data[] data = gson.fromJson(jsonString, Data[].class);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.