Java 遍历嵌套的复杂json对象

Java 遍历嵌套的复杂json对象,java,json,Java,Json,我正在读取一个具有动态结构的JSON字符串。JSON可以有一个object和object的数组,或者两者都有,或者只有键值 我知道,我试着穿越所有的可能性去阅读 我需要在不丢失任何节点/密钥对的情况下读取整个字符串 以下是JSON的一个示例: { "cityId": { "city": "Old Delhi", "cityId": "CTY2", "stateId": "STE1", "statusId": "STA1", "userId": "ONB

我正在读取一个具有动态结构的JSON字符串。JSON可以有一个object和object的数组,或者两者都有,或者只有键值

我知道,我试着穿越所有的可能性去阅读

我需要在不丢失任何节点/密钥对的情况下读取整个字符串

以下是JSON的一个示例:

{
  "cityId": {
    "city": "Old Delhi",
    "cityId": "CTY2",
    "stateId": "STE1",
    "statusId": "STA1",
    "userId": "ONB1"
  },
  "emailId": "ud.r@gmail.com",
  "firstName": "Udit",
  "lastName": "R",
  "password1": "xxxxxx",
  "password2": "xxxxxx",
  "statusId": {
    "status": "ACTIVE",
    "statusId": "STA1"
  },
  "userId": "ONB2",
  "userInfoId": {
    "deptId": {
      "deptId": "DPT2",
      "deptName": "HR",
      "statusId": "STA1",
      "userId": "ONB1"
    },
    "roleId": {
      "roleId": "RLE2",
      "roleName": "Member",
      "statusId": "STA1",
      "userId": "ONB1"
    },
    "statusId": {
      "status": "ACTIVE",
      "statusId": "STA1"
    },
    "userId": "ONB2",
    "userInfoId": "UIN2"
  }
}
我当前的Java方法:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import java.io.IOException;
import java.util.Iterator;
import org.json.JSONObject;

// Omittded class definiton, just the method
public void getRSDataFromMethodTwo(String url) throws IOException {
    // Initiate client and do JSON request
    Client client = Client.create();
    WebResource webResource = client.resource(url);        
    ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
    if (response.getStatus() != 200) 
    {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }
    String output = response.getEntity(String.class);
    System.out.println("Response===get=" + output);

    // Parse JSOn
    JSONObject json = new JSONObject(output);
    Iterator<String> iterator = json.keys();               

    System.out.println("Fields:");
    while (iterator.hasNext()) 
    {
        String x = iterator.next().toString();
        System.out.println(" Keys " + x);                        
    }

    iterator = json.keys();
    while (iterator.hasNext()) 
    {
        String key = iterator.next();
        System.out.println(key + " : " + json.get(key));
        if (json.get(key).toString().startsWith("{")) 
        {                
            JSONObject childJson = new JSONObject(json.get(key).toString());                               
            Iterator<String> childIterator = childJson.keys();
            if (childIterator.hasNext()) 
            {                                        
                while (childIterator.hasNext()) 
                {
                    String chKey = childIterator.next();
                    System.out.println("Child Key "+chKey+ " : "+childJson.get(chKey));                        
                }
            }
        }            
    }
}

编辑-由于我无法读取像子键deptId这样的嵌套对象,因为它是动态字符串,所以我不知道哪个对象具有嵌套对象。

请看一下将Jackson库与@JsonAnySetter一起使用

该输出实际上缺少什么?有什么不对吗?我强烈推荐使用Jackson。有了它,您可以设计一个包装器POJO,它将自动调用整个动态json,将其反序列化为您可以与之交互的POJO。
Info:   firstName : Udit
Info:   lastName : Ranjan
Info:   statusId : {"statusId":"STA1","status":"ACTIVE"}
Info:   Child Key statusId : STA1
Info:   Child Key status : ACTIVE
Info:   emailId : ud.r@gmail.com
Info:   password2 : xxxxxx
Info:   cityId : {"statusId":"STA1","city":"Old Delhi","stateId":"STE1","cityId":"CTY2","userId":"ONB1"}
Info:   Child Key statusId : STA1
Info:   Child Key city : Old Delhi
Info:   Child Key stateId : STE1
Info:   Child Key cityId : CTY2
Info:   Child Key userId : ONB1
Info:   password1 : b2b123
Info:   userId : ONB2
Info:   userInfoId : {"statusId":{"statusId":"STA1","status":"ACTIVE"},"roleId":{"statusId":"STA1","roleId":"RLE2","roleName":"Member","userId":"ONB1"},"deptId":{"deptName":"HR","statusId":"STA1","deptId":"DPT2","userId":"ONB1"},"userId":"ONB2","userInfoId":"UIN2"}
Info:   Child Key statusId : {"statusId":"STA1","status":"ACTIVE"}
Info:   Child Key roleId : {"statusId":"STA1","roleId":"RLE2","roleName":"Member","userId":"ONB1"}
Info:   Child Key deptId : {"deptName":"HR","statusId":"STA1","deptId":"DPT2","userId":"ONB1"}
Info:   Child Key userId : ONB2
Info:   Child Key userInfoId : UIN2