【问题标题】:how to resolve error in reading json?如何解决读取json时的错误?
【发布时间】:2016-03-28 08:49:21
【问题描述】:

下面是我的 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得到了值。

谁能告诉我 - 如何解决上述错误? 非常感谢。

【问题讨论】:

  • 你能告诉我们一个有效的 JSON 吗?您的 JSON 未解析为有效的 JSON。
  • 请把你的原始json数据。
  • N J,这是一个不同的查询 - 我知道如何解析但出现错误。
  • 那么请发布一个格式正确的有效 Json。

标签: android json


【解决方案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 mappedBarcode) {

    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();
    }
}

【讨论】:

  • Satya,上面用到的code.ItemInfo --> [{"chargeable":"True","itemID":"B13984350K"},{"chargeable":"True","itemID": “B13984351A”}] 但它仍然抛出相同的错误:org.json.JSONException:ItemInfo 没有值。它是如此奇怪的错误。
  • 我试过了,它非常适合我。这是dropbox.com/s/q76ux46fi7po8q0/SamplePro.zip?dl=0 的示例
猜你喜欢
  • 2021-11-12
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 2021-10-26
  • 2018-08-03
  • 2017-11-25
相关资源
最近更新 更多