Java JSONArray JSONObject ["NI"] 未找到

标签 java arrays json xml org.json

嘿,我遇到了一个有趣的问题。似乎 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/

相关文章:

java - 泛型类的新对象上的绑定(bind)不匹配

c++ - 越界访问数组不会出错,为什么?

python - 从数组中删除所有零

c - 如何将字符数组转置为 const char* 字符串?

javascript - 我想编写一个异步 JSON 联系人,它将覆盖我的 data.json 文件的内容

使用 Poison 在 Elixir 中对 map 进行 JSON 编码

java - 为什么 CLASSPATH 以 JDK 路径为前缀?

java - 将英尺转换为英寸

python - 在python中将分隔字符串转换为分层JSON

java - 为什么删除换行符后重新插入换行符不起作用?