本文介绍了使用Volley解析嵌套的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我经历了堆栈溢出中的大多数答案,并尝试了一些无效的答案.
I had gone through most of the answers in stack overflow and tried some of the answers which didn't work.
这是SOAP API的响应.
It is a response from a 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中获得的东西
This is what I am getting in the 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/
But I am getting error for the rest of the code. I am testing this with the sample from https://www.simplifiedcoding.net/android-volley-tutorial-fetch-json/
推荐答案
Rec
不是JSONArray
,它是JSONObject
.尝试使用
Rec
is not JSONArray
, it's JSONObject
. Try using
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();
}
这篇关于使用Volley解析嵌套的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!