0

I have the follow json.

{
   foo:{
      id:1
   },
   name:'Albert',
   age: 32
}

How can I deserialize to Java Pojo

public class User {
    private int fooId;
    private String name;
    private int age;
}
diogo beato
  • 175
  • 1
  • 7
  • 1
    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) – BatScream May 20 '15 at 20:58
  • 1
    @BatScream There's no array here, I believe the problem comes from the fact that in the json the id is nested in a foo object, for which there is no POJO. – azurefrog May 20 '15 at 21:00
  • Has nothing like @JsonProperty("foo.id")? – diogo beato May 20 '15 at 21:06
  • 2
    Why can't you change the structure of your POJO? – Vivin Paliath May 20 '15 at 21:09
  • There's an example on [this wiki](http://wiki.fasterxml.com/JacksonInFiveMinutes) which is almost exactly what you're trying to do. Just create an inner `Foo` class in `User`. – azurefrog May 20 '15 at 21:17

3 Answers3

0

You can do one of the following:

  • Create a concrete type representing Foo:

    public class Foo {
        private int id;
    
        ...
    }
    

    Then in User you would have:

    public class User {
        private Foo foo;
    
        ...
    }
    
  • Use a Map<String, Integer>:

    public class User {
        private Map<String, Integer> foo;
    
        ...
    }
    

If other callers are really expecting you to have a getFooId and a setFooId, you can still provide these methods and then either delegate to Foo or the Map depending on the option you choose. Just make sure that you annotate these with @JsonIgnore since they aren't real properties.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

This is what you need to deserialize, using the JsonProperty annotations in your constructor.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class User {

    private int fooId;
    private String name;
    private int age;

    public int getFooId() {
        return fooId;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public User(@JsonProperty("age") Integer age, @JsonProperty("name") String name,
                @JsonProperty("foo") JsonNode foo) {
        this.age = age;
        this.name = name;
        this.fooId = foo.path("id").asInt();
    }

    public static void main(String[] args) {

        ObjectMapper objectMapper = new ObjectMapper();

        String json = "{\"foo\":{\"id\":1}, \"name\":\"Albert\", \"age\": 32}" ;
        try {
            User user = objectMapper.readValue(json, User.class);

            System.out.print("User fooId: " + user.getFooId());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Output:

User fooId: 1

Hope it helps,

Jose Luis

jbarrueta
  • 4,907
  • 2
  • 20
  • 21
-1

You can use a very helpful gson google API.

First of all, create these two classes:

User class:

public class User{
    Foo foo;
    String name;
    int age;
    //getters and setters
} 

Foo class:

public class Foo{
    int id;
    //getters and setters
}

If you have a example.json file then deserialize it as follow

Gson gson = new Gson();
User data = gson.fromJson(new BufferedReader(new FileReader(
        "example.json")), new TypeToken<User>() {
}.getType());

If you have a exampleJson String then deserialize it as follow

Gson gson = new Gson();
User data = gson.fromJson(exampleJson, User.class);
MChaker
  • 2,610
  • 2
  • 22
  • 38