2016-03-28 28 views
0

Aşağıda benim ben (kod Log.i ("callinfo", callInfo + "") okumaya çalışıyorum json vardır;. Itibaren, ama hata alıyorum Kodum da bahsedilmektedir okumak ve hata mesajı içinjson okuma hatası nasıl giderilir?

{ 
    "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" 
} 
.

Kod:.

İşte
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) 

biz ItemInfo değer var açıkça görebilirsiniz

: aşağıdaki satırda

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

hata alınıyor

Herkes bana söyleyebilir mi - yukarıdaki hatayı nasıl giderebilirim? Çok teşekkürler şimdiden.

İşte

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 + "");

tam yöntemini public void readBarCode (String tepkisi, dize scannedBarcode) {olduğu kodunun altına olan

+1

Olası kopyalar [Android'de JSON verilerini ayrıştırmak nasıl ] (http://stackoverflow.com/questions/8178681/how-to-parse-json-data-in-android) –

+1

Bize geçerli bir JSON gösterebilir misiniz? JSON’unuz geçerli bir JSON olarak ayrıştırılamaz. –

+0

Lütfen orijinal json verilerinizi koyun. –

cevap

1

deneyin

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

Satya, yukarıda kod.ItemInfo -> [{"ücretli": "True", "itemID": "B13984350K"}, {"ücretli": "True", "itemID": "B13984351A"}] hala aynı hatayı atar: org.json.JSONException: ItemInfo için değer yok. Onun çok garip hatası. – Dep

+0

Denedim ve benim için mükemmel çalışıyor. İşte https://www.dropbox.com/s/q76ux46fi7po8q0/SamplePro.zip?dl=0 –

+0

örneğim yenilendi ve denedim. Şuan çalışıyor. Çok teşekkürler!! – Dep

İlgili konular