Volley JSONObject 请求解析响应 returns 没有值
Volley JSONObject request Parsing Response returns No Value for
我的本地机器上有一个 RESTful API 运行 return 是 JSON 中的一个响应(实际上这个 JSON 是nodejs Soap 客户端请求的响应)取决于请求。对于这种特殊情况,我收到来自 Android 客户端的 POST 请求和 return 以下响应:
{
QueryAcctBalResponse: {
BalExDtoList: {
BalExDto: [{
BalID: "xxxx",
AcctResID: "xxxx",
AcctResName: "xxxx",
BalType: "xxxx",
Balance: "xxxx",
EffDate: "xxxx",
ExpDate: "xxxx",
UpdateDate: "xxxx"
}, {
BalID: "yyyy",
AcctResID: "yyyy",
AcctResName: "yyyy",
BalType: "yyyy",
Balance: "yyyy",
EffDate: "yyyy",
ExpDate: "yyyy",
UpdateDate: "yyyy"
}]
}
}
}
问题是每次我尝试解析该响应并在 android 中显示此信息(特别是 "AcctResName")。我得到 org.json.JSONException:BalExDto 没有值。我在 android 中使用 Volley Libray 来解析 Json 并且我使用了 Jsonobject request.
请求代码。
JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("BalExDto");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
}
pd.hide();
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response", "Response_TAG: "+response);
}
BalExDto
数组不是响应的直接子项。它在 BalExDtoList
里面,在 QueryAcctBalResponse
对象里面。不能直接访问JSON中的任何节点,需要遍历完整路径才能访问。
你需要得到如下数组
JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
那是因为你没有JSON数组,你有JSON对象。
首先,您需要从 API 响应中获取 JSON 对象,
JSONObject jsonObject = response.getJSONObject("QueryAcctBalResponse")
JSON 对象内部是 JSON 数组。
JSONArray jsonArray = response.getJSONArray("BalExDtoList");
错误提示 - 您解析 BalExDto
错误。 BalExDto
不是 response
的直系子代。所以你必须解析QueryAcctBalResponse
和BalExDtoList
然后你可以得到BalExDto
。因为 BalExDto
在 BalExDtoList
里面,而 BalExDtoList
在 QueryAcctBalResponse
里面。
JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
}
pd.hide();
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response", "Response_TAG: "+response);
}
我的本地机器上有一个 RESTful API 运行 return 是 JSON 中的一个响应(实际上这个 JSON 是nodejs Soap 客户端请求的响应)取决于请求。对于这种特殊情况,我收到来自 Android 客户端的 POST 请求和 return 以下响应:
{
QueryAcctBalResponse: {
BalExDtoList: {
BalExDto: [{
BalID: "xxxx",
AcctResID: "xxxx",
AcctResName: "xxxx",
BalType: "xxxx",
Balance: "xxxx",
EffDate: "xxxx",
ExpDate: "xxxx",
UpdateDate: "xxxx"
}, {
BalID: "yyyy",
AcctResID: "yyyy",
AcctResName: "yyyy",
BalType: "yyyy",
Balance: "yyyy",
EffDate: "yyyy",
ExpDate: "yyyy",
UpdateDate: "yyyy"
}]
}
}
}
问题是每次我尝试解析该响应并在 android 中显示此信息(特别是 "AcctResName")。我得到 org.json.JSONException:BalExDto 没有值。我在 android 中使用 Volley Libray 来解析 Json 并且我使用了 Jsonobject request.
请求代码。
JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("BalExDto");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
}
pd.hide();
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response", "Response_TAG: "+response);
}
BalExDto
数组不是响应的直接子项。它在 BalExDtoList
里面,在 QueryAcctBalResponse
对象里面。不能直接访问JSON中的任何节点,需要遍历完整路径才能访问。
你需要得到如下数组
JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
那是因为你没有JSON数组,你有JSON对象。
首先,您需要从 API 响应中获取 JSON 对象,
JSONObject jsonObject = response.getJSONObject("QueryAcctBalResponse")
JSON 对象内部是 JSON 数组。
JSONArray jsonArray = response.getJSONArray("BalExDtoList");
错误提示 - 您解析 BalExDto
错误。 BalExDto
不是 response
的直系子代。所以你必须解析QueryAcctBalResponse
和BalExDtoList
然后你可以得到BalExDto
。因为 BalExDto
在 BalExDtoList
里面,而 BalExDtoList
在 QueryAcctBalResponse
里面。
JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
}
pd.hide();
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response", "Response_TAG: "+response);
}