我在这里有示例Json格式,我想使用android volley解析,但我遇到了错误: 在org.json.JSONObject类型的入口处无法转换为JSONArray
{
"entry": {
"": {
"": "http:\/\/www.w3.org\/2005\/Atom",
"m": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices\/metadata",
"d": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices"
},
"FOO_BLOCK": {
"BAR_xmlbase": "https:\/\/ab.com:443\/dap\/opu\/odata\/dap\/BATCH_SRV\/"
},
"id": {
"TEXT": "https:\/\/ab.com:443\/dap\/opu\/odata\/dap\/BATCH_SRV\/BatchSet('1000')"
},
"title": {
"FOO_BLOCK": {
"BAR_type": "text"
},
"TEXT": "BatchSet('1000')"
},
"updated": {
"TEXT": "2018-06-05T13:45:24Z"
},
"category": {
"FOO_BLOCK": {
"BAR_term": "BATCH_SRV.Batch",
"BAR_scheme": "http:\/\/schemas.microsoft.com\/ado\/2007\/08\/dataservices\/scheme"
},
"TEXT": "NULL"
},
"link": {
"FOO_BLOCK": {
"BAR_href": "BatchSet('1000')",
"BAR_rel": "self",
"BAR_title": "Batch"
},
"TEXT": "NULL"
},
"content": {
"FOO_BLOCK": {
"BAR_type": "application\/xml"
},
"mproperties": {
"dCharg": {
"TEXT": 1000
},
"dICharg": {
"TEXT": 1000
},
"dMaktx": {
"TEXT": "No Material description avaibalbe"
},
"dStatus": {
"TEXT": "Batch is unrestricted"
},
"dStock": {
"TEXT": "NULL"
}
}
}
}
}
下面是我在android中编写的代码。
JsonObjectRequest arrReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject >() {
@Override
public void onResponse(JSONObject response) {
Log.e("FD", response.toString());
// Check the length of our response (to see if the user has any repos)
// Process the JSON
try{
// Get the JSON array
JSONArray jsonArray = response.getJSONArray("entry");
Log.e("FD", jsonArray.toString());
// Loop through the array elements
for(int i=0;i<array.length();i++){
// Get current json object
JSONObject data = array.getJSONObject(i);
String Charg = student.getString("Charg");
String Status = student.getString("Status");
// Display the formatted json data in text view
addToRepoList(Charg , Status );
}
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// If there a HTTP error then add a note to our repo list.
setRepoListText("Error while calling REST API");
Log.e("Volley", error.toString());
}
}
);
但是我在Android中解析时遇到了错误。错误是 - JSONObject无法转换为JSONArray
任何人都可以提供示例代码来解析这个json数组。
答案 0 :(得分:1)
您的entry
不是JSONOArray
,而是JSONObject
。在你的json响应中,没有JSONArray
尝试这样的事情:
JSONObject entryObj = response.getJSONObject("entry")
然后:
JSONObject fooBlockObj = entryObj.getJSONObject("FOO_BLOCK")
...
JSONObject contentObj = entryObj.getJSONObject("content")
JSONObject mProperties = contentObj.getJSONObject("mproperties")
JSONObject dCharg = mProperties.getJSONObject("dCharg")
JSONObject dStatus = mProperties.getJSONObject("dStatus")
最后
String charg = dCharg.getString("TEXT")
String status = dStatus.getString("TEXT")
答案 1 :(得分:0)
您的var loggerFactory = new LoggerFactory();
var identityServerCors = new DefaultCorsPolicyService(loggerFactory.CreateLogger<DefaultCorsPolicyService>())
{
AllowAll = true
};
不包含json response
。应该注意的一些问题是
json array
是一个json对象,但您将其解析为数组entry
,这些密钥甚至不在响应中(而是Charg, Status
存在,这可能是您想要的密钥)如果您需要获取密钥dCharg, dStatus
,请检查以下内容
Charg, Status