嘿,我遇到了一个有趣的问题。似乎Json (org.json.JSONArray)找不到元素“ NI” ,即使元素>>>就在其中<<<< / p>
我的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”]。
我俯瞰什么?
答案 0 :(得分:0)
@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");
注意:最好将这样的操作参数化