简体   繁体   中英

Java JSONArray JSONObject[“NI”] not found

Hey all I am running into an interesting problem. Seems that the Json (org.json.JSONArray) can not find the element "NI" even though its >>>right there<<<

My 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>

Into 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>...

As you see "NI" is right there yet it says nope!

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;
    }
}

And how I am calling it (the _tmpRecieved houses the XML string):

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());
    }

And this is the spot where that error shows up:

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

The offical error:

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

What am I overlooking?

@Rogue answered your question.

What you are doing is search the JSONArray "NI" inside the JSONObject "jsondata".

"NI" isn't inside the JSONObject "jsondata", but is in the JSONObject "NIResult". You have to travel to the JSONObject "NIResult" to succesfully get the JSONArray.

You can travel using:

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

Note: is better to parameterize action like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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