代码之家  ›  专栏  ›  技术社区  ›  Code_Singh

如何在java中操作嵌套的Json数据进行反嵌套

  •  2
  • Code_Singh  · 技术社区  · 7 年前

    我一直在寻找这个问题的解决方案,但没有找到一个这样问这个问题的解决方案。
    我有一些像这样的数据

    {
           "data": [
             {
                "id": "5ab892c71810e201e81b9d39",
                "isSignedUpUsingFb": false,
                "personalInformation": {
                    "firstName": "jio",
                    "lastName": "g",
                    "mobileNumber": "1234567890",
                 },
                "accountBalance": 0,
               }
             ]
    },
    

    我想编写一个java代码,将数据结构更改为

    {
               "data": [
                  {
                    "id": "5ab892c71810e201e81b9d39",
                    "isSignedUpUsingFb": false,
                    "personalInformation_firstName":"jio",
                     "personalInformation_lastNAme":"g",
                     "personalInformation_mobileNumber":"1234567890",
    
                    "accountBalance": 0,
                   }
                 ]
    },
    

    我从db获取的数据如下:

           @Override
            public List<User> getAllUsers() {
                logger.debug("entering all users method");
                List<User> allUsers=mongoOperations.findAll(User.class);
                for (User user : allUsers) {
                    PersonalInformation info=user.getPersonalInformation());
                    //manipulation code here
                    user.setPersonalInformation(info);
    
                }
                return allUsers;
            }         
    

    所以我想写一个逻辑,这样我就可以将数据转换成所需的格式,并发送一个返回类型。我知道如何使用J query做同样的事情,但我想在后端做,所以上面的任何代码或任何链接都会有所帮助。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Vipul Singh    7 年前

    我有一个非常简单的解决方案。所以,基本上,当我们为嵌套数据创建对象时,我们在JAVA中是这样创建的。

    public MyClass{
    
    public String name;
    public String contact;
    public PersonalInformation personalinformation;
    //setters and getter here
    }  
    

    这将为我提供以下数据

    "MyClass":{
    "name": "abc",
    "contact": "12345",
    "personalInformation":{
    "address": "asdasdasdad",
    "city":"asdadad",
    "pin": "asdfg",
    }
    }  
    

    所以要删除这个嵌套数据,我们需要使用 @JsonUnwrapped 它删除所有嵌套对象并将其添加到主对象。

    public MyClass{
    
        public String name;
        public String contact;
        @JsonUnwrapped
        public PersonalInformation personalinformation;
        //setters and getter here
        }   
    

    将数据结构更改为:

    "MyClass":{
        "name": "abc",
        "contact": "12345",
        "address": "asdasdasdad",
        "city":"asdadad",
        "pin": "asdfg",
        }   
    

    有关更多参考,请查看此链接 http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html
    希望这有帮助。

        2
  •  0
  •   nmanh    7 年前

    有多种可能的解决方案。正如Prabhav所提到的,最直观的方法是创建一个新类,并从中创建一个可以通过库转换为JSON的对象。

    变体一:

    新类看起来像您想要的数据结构,访问权限是:

    PersonalInformationJson pf = new PersonalInformationJson();
    pf.setFirstName = info.getPersonalInformation_firstName
    //... setting the rest of the object
    //using jackson
    ObjectMapper mapper = new ObjectMapper();
    try {
        // convert user object to json string and return it 
       String jsonString = mapper.writeValueAsString(u);
    }
    

    另一个更容易创建字符串的版本,可以按手创建,也可以使用lib创建:

    // using org.json.JSONObject
      String jsonString = new JSONObject().put("personalInformation_firstName", info.value())
                .put("personalInformation_lastNAme", info.value());