JSON编组/解组包含对象列表的对象(Java,JSON / Jackson)

时间:2016-10-11 08:36:57

标签: java json jackson

我认为这将是一个简单的问题。

我有一个简单的结构,一个班级"人员"包含" SimplePerson"的列表对象。 Persons.java看起来像这样:

package jsontests;

import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName (value="persons")
  public class Persons {

  @JsonProperty("person")
  private List< SimplePerson> l;

  public Persons(List<SimplePerson> pl) {
      this.l = pl;
  }

  public Persons() {
      this.l = new ArrayList<>();
  }

  public List<SimplePerson> getL() {
      return this.l;
  }

  public void setL(List<SimplePerson> l) {
      this.l = l;
  }
}

&#34; SimplePerson.java&#34;看起来像这样:

package jsontests;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName (value="person")
public class SimplePerson {

  String name;
  String firstname;


  /**
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * @param name the name to set
   */
  public void setName(String name) {
      this.name = name;
  }

  /**
   * @return the firstname
   */
  public String getFirstname() {
      return firstname;
  }

  /**
   * @param firstname the firstname to set
   */
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
}

我使用此代码片段来创建这些对象并对其进行编组:

package jsontests;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonListTest {

  public static void main(String[] args) {
      JsonListTest var = new JsonListTest();
      var.run();
  }

  private void run() {
      ObjectMapper mapper = new ObjectMapper();
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
      Persons cl = new Persons();

      SimplePerson first = new SimplePerson();
      first.setName("Schmidt");
      first.setFirstname("Peter");
      cl.getL().add(first);
      SimplePerson second = new SimplePerson();
      second.setName("Smith");
      second.setFirstname("George");

      cl.getL().add(second);

      String s = null;
      try {
          s = mapper.writeValueAsString(cl);
      } catch (JsonProcessingException e) {
          e.printStackTrace();
      }
      System.out.println(s);
  }
}

结果如预期:

{
"persons" : {
  "person" : [ {
    "name" : "Schmidt",
    "firstname" : "Peter"
  }, {
    "name" : "Smith",
    "firstname" : "George"
  } ]
  }
}

在我的脑海里,它读取&#34;我们有一个名为人的对象,其中包含一个名为person的对象。此person-object包含由名称/名字对组成的对象数组。&#34;这一切对我来说完全没问题,而且正是代码

不幸的是,我从客户那里得到的东西看起来有点不同。我收到了

{
  "persons": [
     {
       "person": {
          "name" : "Schmidt",
          "firstname": "Peter"
        }
     },{
        "person": {
          "name" : "Smith",
          "firstname": "George"
        }
     }
  ]
}

在我看来,这看起来有点不同:&#34;我们有一个名为persons的对象,它包含一个数组。该数组的第一个和第二个元素是名为person的对象,它们由名字/名字对组成。

同样但不同:-)我花了一天时间找到一种方法来消耗我得到的输入 - 而且我没有成功。改变我的类结构没有问题 - 说服客户提供不同的JSON的可能性很小。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:-1)

我遵循了Derick的建议(参见第一条评论)并生成了所需的数据模型。这似乎是最好的解决方案。