Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

java - JSON Jackson deserialization multiple keys into same field

I am trying to convert JSON into POJO. I have worked with Jackson to convert standard JSON file. In this particular case, I would like to overwrite the key value to "default" class/variable. In this case, there are multiple key value to be replaced (ie. hundreds, and the key values to be replaced are unknown).

Is this possible? I thought of storing it into Map, then iterate and store each into POJO, but wondering if there is different option, since I am not that familiar with storing JSON to Map.

Example of the JSON to be processed:

"People" : {
    "person1" : { 
      "name" : "john doe",
      "address" : "123 main st",
      "email" : "[email protected]"
    },
    "person2" : { 
      "name" : "bob cat",
      "address" : "234 dog st",
      "email" : "[email protected]"
    },
    "person3" : { 
      "name" : "foo bar",
      "address" : "111  1st ave",
      "email" : "[email protected]"
    },
    "person8" : { 
      "name" : "james bono",
      "address" : "999 alaska st",
      "email" : "[email protected]"
    }
}

Is it possible to generate the class in the following structure? The main issue is there are hundreds of value to be replaced and assuming they are unknown, I can't use this approach.

@JsonIgnoreProperties(ignoreUnknown = true)
public class People { 

  @JsonAlias({"person1", "person2"})
  private List<Details> person; // --> this should be the default replacing person1, person2, and so on 

  private class Details { 
    String name;
    String address;
    String email;
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use JsonAnySetter annotation for all properties personXYZ. See below example:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);

        System.out.println(mapper.readValue(jsonFile, People.class).getPersons());
    }
}

class People {

    private List<Details> persons = new ArrayList<>();

    @JsonAnySetter
    public void setPerson(String name, Details person) {
        this.persons.add(person);
    }

    public List<Details> getPersons() {
        return persons;
    }

    public static class Details {
        String name;
        String address;
        String email;

        // getters, setters, toString
    }
}

For your JSON above code prints:

[Details{name='john doe', address='123 main st', email='[email protected]'}, Details{name='bob cat', address='234 dog st', email='[email protected]'}, Details{name='foo bar', address='111  1st ave', email='[email protected]'}, Details{name='james bono', address='999 alaska st', email='[email protected]'}]

In case you use inner class remember to make it public static to make it visible to Jackson instantiation process.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...