0

I am having a really hard time understanding how to place a mapped valued into an array and how to iterate that array.

test.json

 [
   {
     "Name": "Bob",
     "Nationality": "",
     "City": "Chicago",
     "Phone" : "451232424234"
   },

    ......continues with more objects
 ]

testTemplate.java

 //imports
  @JSONInclude(Json.Include.Non_NULL)
  @JsonPropertyOrder({"Name,"Nationality","City","Phone"})

  Public class testTemplate {

  @JsonProperty("Name")
  private String userName;

  @JsonProperty("Nationality")
  private String nation;

  @JsonProperty("City")
  private String city;

  @JsonProperty("Phone")
  private String phone;

  @JsonProperty("Name")
  public String getName (String userName) {
      this.userName = userName;
  }

  @JsonProperty("Nationality")
  public String nation (String nation) {
      this.nation = nation;
  }

  @JsonProperty("City")
  public String city (String city) {
      this.city = city;
  }

  @JsonProperty("Phone")
  public String phone (String phone) {
      this.phone = phone;
  }

  public String toString() {
   return ToStringBuilder.reflectionToString(this);
  }

testParse.java

Public Class testParse {
   List<testParse> test;
   ObjectMapper mapper;

   protected void setUp() throws IOException {
          mapper = new ObjectMapper();
          mapper.enable(SerializationFeature.INDENT_OUTPUT();
          test = mapper.readValue(this.getClass().getResourcesAsStream("test.json"),
          mapper.getTypeFactory().constructCollectionType(List.class, testParse.class));

I need to help first clarifying exactly what the code is doing, and how to put JSON properties (Name, Nationality,City,Phone) into Java.

My understanding is the testTemplate file create the strings in which the properties will be held, then the testParse file has the mapper feature (from Jackson) that reads through the json and puts all into "test" (as an array?)

My goal is in testParse, where if everything thing is in "test", then I read through that, and start to pull it out and place it into a folderList.

 public static Map<String, String> userName throws IOException {
     Map<String, String> folderList = new TreeMap<>();
     //Don't know how, but iterate through "test"
     LineIterator it = new LineIterator(//the method used to read the json file);
     try {
         while(it.hasNext()) {
         String line = it.nextLine();
         folderList.put(userName,nation) //can I also put city and phone at once as well or should I create another put: folderList.put(userName, city)
         return folderList; 

How does one do this? Is there a better way to put the properties of json into the folderList after using the jackson mapper?

Bobshark
  • 376
  • 1
  • 3
  • 12

1 Answers1

1

Actually, testTemplate don't generate anything, what Jackson have done here is just get data from "test.json" as String, then using Reflection read the format of testTemplate.java. Based on template it've just have + setting you add to ObjectMapper object, Jackson will convert test.json to array of object Java.
P/S: you don't need to add annotation in both attributes and get function of POJO class, just do it in get function or attributes only, it's enough for Jackson.

Huy Tran
  • 599
  • 1
  • 10
  • 19
  • Can you please show how to add a object to the folderList array in testParse.java. – Bobshark Jun 28 '16 at 16:49
  • you can follow this to get it http://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects – Huy Tran Jun 29 '16 at 11:34