>> 就在那里 -6ren">
gpt4 book ai didi

Java JSONArray JSONObject ["NI"] 未找到

转载 作者:行者123 更新时间:2023-12-02 01:38:53 24 4
gpt4 key购买 nike

嘿,我遇到了一个有趣的问题。似乎 Json (org.json.JSONArray) 找不到元素 "NI" 即使它 >>> 就在那里<<<

我的 XML:

  <soap:Header>  </soap:Header>
<soap:Body>
<Multi_search_NI_response xmlns:sear="http://www.identitysystems.com/xmlschema/iss-version-11/searchSvc" response="0">
<NIResult>
<NI>
<score>100</score>
<ID>64973020020</ID>
<FULL_NAME>Bob Showcase Barker</FULL_NAME>
<FIRST_NAME>Bob</FIRST_NAME>
<LAST_NAME>Barker</LAST_NAME>
<DOB>11/23/1982</DOB>
<CL_ID/>
</NI>
<NI>
<score>87</score>
<ID>54619738215</ID>
<FULL_NAME>Steve apple jobs</FULL_NAME>
<FIRST_NAME>Steve</FIRST_NAME>
<LAST_NAME>Jobs</LAST_NAME>
<DOB>10/22/1992</DOB>
<CL_ID/>
</NI>
</NIResult>
</Multi_search_NI_response>
</soap:Body>

转换为 JSON:

{
"soap:Header": "",
"soap:Body": {
"Multi_search_NI_response": {
"xmlns:sear": "http://www.identitysystems.com/xmlschema/iss-version-11/searchSvc",
"response": 0,
"NIResult": {
"NI": [{
"CL_ID": "",
"LAST_NAME": "Barker",
"FIRST_NAME": "Bob",
"score": 100,
"NRI_ID": 64973020020,
"DOB": "11/23/1982",
"FULL_NAME": "Bob Showcase Barker"
}, {
"CL_ID": "",
"LAST_NAME": "Jobs",
"FIRST_NAME": "Steve",
"score": 87,
"NRI_ID": 54619738215,
"DOB": "11/23/1982",
"FULL_NAME": "Steve apple Jobs"
}
]
}
}
}
}

Jsondata = {"soap:Header":"","soap:Body":{"Multi_search_NI_response":{"xm...

path = C:\Repository\xml2JsonPretty/DownloadedXML/2

_tmpRecieved = <soap:Header></soap:Header><soap:Body><Multi_search_NI_response xmlns:sear="http://www.identitysystems.com/xmlschema/iss-version-11/searchSvc" response="0"><NIResult><NI><score>100</score><ID>64973020020</ID>...

如您所见,“NI”就在那里,但它说不!

final class MyResult {
final File file;
final String csv;
final CsvToExcel excel;

public MyResult(File file, String csv, CsvToExcel excel) {
this.file = file;
this.csv = csv;
this.excel = excel;
}

public File getFile() { return file; }
public String getCSV() { return csv; }
public CsvToExcel getExcel() { return excel; }
}

private static MyResult CSVFromJSON(JSONObject jsondata, String path) {
try {
//Now create CSV from JSON:
JSONArray docs = jsondata.getJSONArray("NI");
File file = new File(CSVName);
String csv = CDL.toString(docs);
xml2json x2j = new xml2json();
CsvToExcel excel = x2j.new CsvToExcel();
MyResult myR = x2j.new MyResult(file, csv, excel);

return myR;
} catch (JSONException e) {
e.printStackTrace();

return null;
}
}

以及我如何调用它(_tmpRecieved 包含 XML 字符串):

try {
JSONObject jsondata = CSVToJSON(_tmpRecieved);
MyResult csvdata = CSVFromJSON(jsondata, path);
String ExcelFilePath = CSVToExcel(csvdata, path);

System.out.println("ExcelPath: " + ExcelFilePath);
} catch (Exception ex) {
System.out.println(ex.toString());
}

这就是错误出现的地方:

JSONArray docs   = jsondata.getJSONArray("NI");

官方错误:

org.json.JSONException: JSONObject["NI"] not found.

我忽略了什么?

最佳答案

@Rogue 回答了您的问题。

您正在做的是在 JSONObject“jsondata”内搜索 JSONArray“NI”。

“NI”不在 JSONObject“jsondata”内,在 JSONObject“NIResult”内。您必须前往 JSONObject“NIResult”才能成功获取 JSONArray。

您可以使用以下方式出行:

JSONObject soapBody = jsondata.getJSONObject("soap:Body");
JSONObject response = soapBody.getJSONObject("Multi_search_NI_response");
JSONObject result = response.getJSONObject("NIResult");
JSONArray docs = result.getJSONArray("NI");

注意:最好像这样参数化操作

关于Java JSONArray JSONObject ["NI"] 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500277/

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