Am having the String of json array as follow
{"Compemployes":[ { "id":1001, "name":"jhon" }, { "id":1002, "name":"jhon" } ]}
i want to convert this this jsonarray to List<Empolyee>
. for this i had added the the maven dependency "camel-jackson
" and also write the pojo class for employee . but when i try to run my below code
ObjectMapper mapper = new ObjectMapper(); List<Employe> list = mapper.readValue(jsonString, TypeFactory.collectionType(List.class, Employe.class));
am getting the following exception.
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@43caa144; line: 1, column: 1]
can someone pls tell what am missing or doing anyting wrong
We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.
JSONArray objects have a function getJSONObject(int index) , you can loop through all of the JSONObjects by writing a simple for-loop: JSONArray array; for(int n = 0; n < array. length(); n++) { JSONObject object = array. getJSONObject(n); // do some stuff.... }
Java For TestersWe can convert a JSON array to a list using the ObjectMapper class. It has a useful method readValue() which takes a JSON string and converts it to the object class specified in the second argument.
JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.
The problem is not in your code but in your json:
{"Compemployes":[{"id":1001,"name":"jhon"}, {"id":1002,"name":"jhon"}]}
this represents an object which contains a property Compemployes which is a list of Employee. In that case you should create that object like:
class EmployeList{ private List<Employe> compemployes; (with getter an setter) }
and to deserialize the json simply do:
EmployeList employeList = mapper.readValue(jsonString,EmployeList.class);
If your json should directly represent a list of employees it should look like:
[{"id":1001,"name":"jhon"}, {"id":1002,"name":"jhon"}]
Last remark:
List<Employee> list2 = mapper.readValue(jsonString, TypeFactory.collectionType(List.class, Employee.class));
TypeFactory.collectionType
is deprecated you should now use something like:
List<Employee> list = mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, Employee.class));
/* It has been answered in http://stackoverflow.com/questions/15609306/convert-string-to-json-array/33292260#33292260 * put string into file jsonFileArr.json * [{"username":"Hello","email":"[email protected]","credits" * :"100","twitter_username":""}, * {"username":"Goodbye","email":"[email protected]" * ,"credits":"0","twitter_username":""}, * {"username":"mlsilva","email":"[email protected]" * ,"credits":"524","twitter_username":""}, * {"username":"fsouza","email":"[email protected]" * ,"credits":"1052","twitter_username":""}] */ public class TestaGsonLista { public static void main(String[] args) { Gson gson = new Gson(); try { BufferedReader br = new BufferedReader(new FileReader( "C:\\Temp\\jsonFileArr.json")); JsonArray jsonArray = new JsonParser().parse(br).getAsJsonArray(); for (int i = 0; i < jsonArray.size(); i++) { JsonElement str = jsonArray.get(i); Usuario obj = gson.fromJson(str, Usuario.class); //use the add method from the list and returns it. System.out.println(obj); System.out.println(str); System.out.println("-------"); } } catch (IOException e) { e.printStackTrace(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With