繁体   English   中英

找不到Java JSONArray JSONObject [“ NI”]

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM