How to parse a JSON string to an array using Jackson

JavaJsonJackson

Java Problem Overview


I have a String with the following value:

[  {    "key1": "value11",    "key2": "value12"  },  {    "key1": "value21",    "key2": "value22"  }]

And the following class:

public class SomeClass {
    private String key1;
    private String key2;
    /* ... getters and setters omitted ...*/
}

And I want to parse it to a List<SomeClass> or a SomeClass[]

Which is the simplest way to do it using Jackson ObjectMapper?

Java Solutions


Solution 1 - Java

I finally got it:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));


	

Solution 2 - Java

The other answer is correct, but for completeness, here are other ways:

List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });
SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);

Solution 3 - Java

The complete example with an array. Replace "constructArrayType()" by "constructCollectionType()" or any other type you need.

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class Sorting {

    private String property;

    private String direction;

    public Sorting() {

    }

    public Sorting(String property, String direction) {
        this.property = property;
        this.direction = direction;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public static void main(String[] args) throws JsonParseException, IOException {
        final String json = "[{\"property\":\"title1\", \"direction\":\"ASC\"}, {\"property\":\"title2\", \"direction\":\"DESC\"}]";
        ObjectMapper mapper = new ObjectMapper();
        Sorting[] sortings = mapper.readValue(json, TypeFactory.defaultInstance().constructArrayType(Sorting.class));
        System.out.println(sortings);
    }
}

Solution 4 - Java

I sorted this problem by verifying the json on JSONLint.com and then using Jackson. Below is the code for the same.

 Main Class:-

String jsonStr = "[{\r\n" + "		\"name\": \"John\",\r\n" + "		\"city\": \"Berlin\",\r\n"
				+ "	        \"cars\": [\r\n" + "			\"FIAT\",\r\n" + "			\"Toyata\"\r\n"
				+ "		],\r\n" + "		\"job\": \"Teacher\"\r\n" + "	},\r\n" + "	{\r\n"
				+ "		\"name\": \"Mark\",\r\n" + "		\"city\": \"Oslo\",\r\n" + "		\"cars\": [\r\n"
				+ "			\"VW\",\r\n" + "			\"Toyata\"\r\n" + "		],\r\n"
				+ "		\"job\": \"Doctor\"\r\n" + "	}\r\n" + "]";

		ObjectMapper mapper = new ObjectMapper();

		MyPojo jsonObj[] = mapper.readValue(jsonStr, MyPojo[].class);

		for (MyPojo itr : jsonObj) {

			System.out.println("Val of getName is: " + itr.getName());
			System.out.println("Val of getCity is: " + itr.getCity());
			System.out.println("Val of getJob is: " + itr.getJob());
			System.out.println("Val of getCars is: " + itr.getCars() + "\n");

		}

POJO:

public class MyPojo {

private List<String> cars = new ArrayList<String>();

private String name;

private String job;

private String city;

public List<String> getCars() {
	return cars;
}

public void setCars(List<String> cars) {
	this.cars = cars;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public String getJob() {
	return job;
}

public void setJob(String job) {
	this.job = job;
}

public String getCity() {
	return city;
}

public void setCity(String city) {
	this.city = city;
} }

  RESULT:-
         Val of getName is: John
         Val of getCity is: Berlin
         Val of getJob is: Teacher
         Val of getCars is: [FIAT, Toyata]

          Val of getName is: Mark
          Val of getCity is: Oslo
          Val of getJob is: Doctor
          Val of getCars is: [VW, Toyata]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmmutilvaView Question on Stackoverflow
Solution 1 - JavammutilvaView Answer on Stackoverflow
Solution 2 - JavaStaxManView Answer on Stackoverflow
Solution 3 - JavaDidaskoView Answer on Stackoverflow
Solution 4 - JavaAtul Kumar SharmaView Answer on Stackoverflow