使用Volley解析嵌套的JSON对象

贾扬(Jayan)开发

我经历了堆栈溢出中的大多数答案,并尝试了一些无效的答案。

它是来自SOAP API的响应。

我尝试使用

JSONObject obj = new JSONObject(response);
JSONArray heroArray = new JSONArray();
JSONObject one = obj.getJSONObject("getProjectDetailsResult");
JSONObject two = one.getJSONObject("NewDataSet");
heroArray= two.getJSONArray("Rec");

for (int i = 0; i < heroArray.length(); i++) {
    JSONObject heroObject = heroArray.getJSONObject(i);
    Hero hero = new Hero(heroObject.getString("decProjectID"), 
heroObject.getString("chvProjectNameEng"));

这就是我在LogCat中得到的

2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err: org.json.JSONException: Value {"decProjectID":"100300230049","intProjectSlNo":"49",......"percentage":"0"} at Rec of type org.json.JSONObject cannot be converted to JSONArray
2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
2019-12-08 15:00:47.572 6134-6134/net.marvelheroes W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:588)

我试过了

JSONObject heroArray = new JSONObject();
JSONObject one = obj.getJSONObject("getProjectDetailsResult");
JSONObject two = one.getJSONObject("NewDataSet");
heroArray= two.getJSONObject("Rec");

但是我在其余的代码中遇到了错误。我正在使用https://www.simplifiedcoding.net/android-volley-tutorial-fetch-json/中的示例对此进行测试

阿萨杜扎曼医师

Rec不是JSONArray,是JSONObject尝试使用

try {
    JSONObject obj = new JSONObject(response);
    JSONObject one = obj.getJSONObject("getProjectDetailsResult");
    JSONObject two = one.getJSONObject("NewDataSet");

    if(two.get("Rec") instanceof JSONArray) {

        JSONArray heroArray = two.getJSONArray("Rec");

        for (int i = 0; i < heroArray.length(); i++) {

            JSONObject heroObject = heroArray.getJSONObject(i);

            Hero hero = new Hero(heroObject.getString("decProjectID"),
                heroObject.getString("intProjectSlNo"),
                heroObject.getString("chvProjectName"),
                heroObject.getString("chvProjectNameEng"),
                heroObject.getString("chrProjCatCode"),
                heroObject.getString("chvEngProjCategory"),
                heroObject.getString("nchvSecType"),
                heroObject.getString("chvEngSecType"),
                heroObject.getString("chvImplOfficerDesg"),
                heroObject.getString("chvImplOfficerDesgEng"),
                heroObject.getString("singleYrAmt"),
                heroObject.getString("TotExp"),
                heroObject.getString("percentage"));

            heroList.add(hero);
        }
    } else {
        JSONObject heroObject = two.getJSONObject("Rec");

        Hero hero = new Hero(heroObject.getString("decProjectID"),
            heroObject.getString("intProjectSlNo"),
            heroObject.getString("chvProjectName"),
            heroObject.getString("chvProjectNameEng"),
            heroObject.getString("chrProjCatCode"),
            heroObject.getString("chvEngProjCategory"),
            heroObject.getString("nchvSecType"),
            heroObject.getString("chvEngSecType"),
            heroObject.getString("chvImplOfficerDesg"),
            heroObject.getString("chvImplOfficerDesgEng"),
            heroObject.getString("singleYrAmt"),
            heroObject.getString("TotExp"),
            heroObject.getString("percentage"));

        heroList.add(hero);
    }
} catch ( Exception ex) {
    ex.printStackTrace();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章