使用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条评论
登录后参与评论

相关文章

来自分类Dev

使用Volley在Android中嵌套JSON对象解析

来自分类Dev

使用Volley解析内部带有JSON数组的JSON对象

来自分类Dev

如何使用DataContractJsonSerializer解析嵌套的json对象?

来自分类Dev

如何使用DataContractJsonSerializer解析嵌套的json对象?

来自分类Dev

如何使用Volley获取和解析JSON对象

来自分类Dev

Javascript解析嵌套的JSON对象

来自分类Dev

Javascript解析嵌套的JSON对象

来自分类Dev

解析包含嵌套对象的JSON

来自分类Dev

无法解析嵌套的 JSON 对象

来自分类Dev

快速解析嵌套的json对象

来自分类Dev

如何使用嵌套数组将JSON解析为对象

来自分类Dev

使用Gson解析无键的嵌套JSON对象

来自分类Dev

在node.js中使用BodyParser解析的JSON嵌套对象

来自分类Dev

如何在Android中使用Retrofit解析嵌套的json对象

来自分类Dev

使用重复键(PHP或JS)解析嵌套的JSON对象

来自分类Dev

使用Gson解析无键的嵌套JSON对象

来自分类Dev

使用Angular.js解析JSON中的嵌套对象数组

来自分类Dev

如何使用GSON解析多个嵌套的JSON对象/数组?

来自分类Dev

使用 GSON 将 JSON 嵌套对象解析为 Java POJO

来自分类Dev

使用Volley进行Android JSON解析

来自分类Dev

如何使用Volley解析JSON数据

来自分类Dev

使用 volley 在 android 中检索 Json 对象

来自分类Dev

使用JavaScript解析嵌套的json

来自分类Dev

使用JQuery解析嵌套的JSON

来自分类Dev

使用JavaScript解析嵌套的json

来自分类Dev

使用 PHP 解析嵌套的 JSON

来自分类Dev

使用 Codable 解析嵌套的 JSON

来自分类Dev

使用 Vuejs 解析嵌套的 JSON

来自分类Dev

json.net-无法解析嵌套对象