[英]Java JSONArray JSONObject[“NI”] not found
嘿,我遇到了一個有趣的問題。 似乎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 ...
路徑= 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”]。
我在俯視什么?
@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");
注意:最好將這樣的操作參數化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.