gpt4 book ai didi

java - 迭代嵌套的复杂 json 对象

转载 作者:行者123 更新时间:2023-12-02 13:09:06 26 4
gpt4 key购买 nike

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

我尝试遍历所有我知道的可能性来阅读。

我需要读取整个字符串而不丢失任何节点/键对。

以下是 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));
}
}
}
}
}

输出:

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

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

最佳答案

看看如何使用 Jackson 库和 @JsonAnySetter http://www.cowtowncoder.com/blog/archives/2011/07/entry_458.html

关于java - 迭代嵌套的复杂 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44024145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com