下面是我的json,我正在尝试阅读(代码Log.i(" callinfo",callInfo +"");以后,但是收到错误。我的代码改为还提到了读取和错误消息。
{
"CallInfo":[
{
"ItemInfo":[
{
"chargeable":"True",
"itemID":"B13984350K"
},
{
"chargeable":"True",
"itemID":"B13984351A"
}
],
"numberOfCopies":2
}
],
"ISBN":[
""
],
"TitleAvailabilityInfo":null,
"author":"Chief Army Medical Officer.",
"baseCallNumber":"RC87.1 PRE",
"publisherName":"HQ Army Medical Services,",
"title":"Preventing heat injuries : the commanders' guide",
"titleID":9206,
"yearOfPublication":"2000"
}
代码:
public void readBarCode(String response, String scannedBarcode) {
final CountDownLatch latch = new CountDownLatch(1);
final String[] names = new String[4];
JSONArray mArray, mArray1, mArray2;
int totalCount = 0;
int avail = 0;
String author, title, publisherName;
try {
JSONObject obj = new JSONObject(response);
//Results
if (obj.getJSONObject("Results") != null) {
JSONObject obj1 = obj.getJSONObject("Results");
//LookupTitleInfoResponse
if (obj1.getJSONObject("LookupTitleInfoResponse") != null) {
JSONObject obj2 = obj1.getJSONObject("LookupTitleInfoResponse");
//TitleInfo
if (obj2.getJSONArray("TitleInfo") != null) {
mArray = obj2.getJSONArray("TitleInfo");
JSONObject callInfo = mArray.getJSONObject(0);
Log.i("callinfo", callInfo + "");
mArray2 = callInfo.getJSONArray("ItemInfo");
for (int i = 0; i <= mArray2.length(); i++) {
if (mArray2.getJSONObject(i).getString("chargeable").equals("False")) {
totalCount++;
}
if (mArray2.getJSONObject(i).getString("itemID").equals(scannedBarcode)) {
avail = 1;
}
}
author = mArray.getJSONObject(0).getString("author");
publisherName = mArray.getJSONObject(0).getString("publisherName");
title = mArray.getJSONObject(0).getString("title");
TitleTxt.setText(title);
PublisherTxt.setText(publisherName);
CreatorTxt.setText(author);
BookBarcode.setText(scannedBarcode);
AvailabiltyTxt.setText(totalCount);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
在下面的行中收到错误:
mArray2 = callInfo.getJSONArray("ItemInfo");
Error:
org.json.JSONException: No value for ItemInfo
03-28 16:33:09.953 17229-17229/com.androidatc.customviewindrawer W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
03-28 16:33:09.953 17229-17229/com.androidatc.customviewindrawer W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
在这里我们可以清楚地看到ItemInfo有价值。
有谁能告诉我 - 如何解决上述错误? 非常感谢提前。
答案 0 :(得分:1)
尝试使用以下代码
mArray = obj2.getJSONArray("TitleInfo");
JSONObject titleInfo = mArray.getJSONObject(0);
JSONArray arr1 = titleInfo.getJSONArray("CallInfo");
JSONObject callInfo = arr1.getJSONObject(0);
JSONArray arr2 = callInfo.getJSONArray("ItemInfo");
Log.i("ItemInfo", arr2 + "");
这是完整的方法 public void readBarCode(String response,String scansBarcode){
final CountDownLatch latch = new CountDownLatch(1);
final String[] names = new String[4];
JSONArray mArray, mArray1, mArray2;
int totalCount = 0;
int avail = 0;
String author, title, publisherName;
try {
JSONObject obj = new JSONObject(response);
//Results
if (obj.getJSONObject("Results") != null) {
JSONObject obj1 = obj.getJSONObject("Results");
//LookupTitleInfoResponse
if (obj1.getJSONObject("LookupTitleInfoResponse") != null) {
JSONObject obj2 = obj1.getJSONObject("LookupTitleInfoResponse");
//TitleInfo
if (obj2.getJSONArray("TitleInfo") != null) {
mArray = obj2.getJSONArray("TitleInfo");
JSONObject titleInfo = mArray.getJSONObject(0);
JSONArray arr1 = titleInfo.getJSONArray("CallInfo");
JSONObject callInfo = arr1.getJSONObject(0);
JSONArray arr2 = callInfo.getJSONArray("ItemInfo");
Log.i("ItemInfo", arr2 + "");
for (int i = 0; i < arr2.length(); i++) {
if (arr2.getJSONObject(i).getString("chargeable").equals("False")) {
totalCount++;
}
if (arr2.getJSONObject(i).getString("itemID").equals(scannedBarcode)) {
avail = 1;
}
}
author = mArray.getJSONObject(0).getString("author");
publisherName = mArray.getJSONObject(0).getString("publisherName");
title = mArray.getJSONObject(0).getString("title");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}