-3

I have JSON string like below, and want to convert into Java object using jackson API.

 [
      {
        "Users": {
          "id": "1",
          "userId": "1424",
          "Firstname": "lms",

        }
      },
      {
        "Users": {
          "id": "2",
          "userId": "4527",
          "Firstname": "Matthew",
         }
      }
 ]

Can you please help me out.

PMerlet
  • 2,568
  • 4
  • 23
  • 39
Ankur jain
  • 963
  • 3
  • 14
  • 21
  • 3
    try google, there must be a thousand examples just on SO – Scary Wombat Dec 06 '16 at 07:28
  • 1
    e.g. http://stackoverflow.com/questions/17371134/java-json-parsing-with-jackson – Scary Wombat Dec 06 '16 at 07:30
  • 4
    Possible duplicate of [How to use Jackson to deserialise an array of objects](http://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects) – PMerlet Dec 06 '16 at 07:30
  • I googled out and found the answer. Thanks all for help!! http://stackoverflow.com/questions/19333106/jsonmappingexception-out-of-start-array-token – Ankur jain Dec 06 '16 at 07:50

2 Answers2

0

This is the solution, works like charm:

try {
                TypeFactory typeFactory = mapper.getTypeFactory();
                CollectionType collectionType = typeFactory.constructCollectionType(
                                                    List.class, Users.class);
                List<Users> usersList =  mapper.readValue(new File("list.json"), collectionType);     

            } catch (IOException e) {
                e.printStackTrace();
            }
Ankur jain
  • 963
  • 3
  • 14
  • 21
-1

Google is your friend, this example shows a good demo

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'mkyong'}";

//JSON from String to Object
User user = mapper.readValue(jsonInString, User.class);
Jerry
  • 98
  • 1
  • 9