2016-07-26 209 views
-1

我有問題;我無法從jsonObject讀取內部字符串。它說JsonArray不能轉換成JsonObject。從JsonObject中讀取Json字符串android

07-26 13:01:31.910 1798-1901/com.example.phuluso.aafs I/System.out: [{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}] 

這是我的JsonArray。我試圖從AccommoAddress閱讀,但我得到的錯誤如下:

[{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}] 

這裏是我的代碼

@Override 
    protected void onPostExecute(String result) { 

     progressDialog.dismiss(); 
     List<AccommoNearAPK> data = new ArrayList<>(); 
     progressDialog.dismiss(); 

     JSONObject jsonResponse = null; 

     try 
     { 
      jsonResponse = new JSONObject(result); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress"); 

      /*********** Process each JSON Node ************/ 

      int lengthJsonArr = jsonMainNode.length(); 

      for(int i=0; i < lengthJsonArr; i++) 
      { 
       /****** Get Object for each JSON node.***********/ 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 

       /******* Fetch node values **********/ 
       String name  = jsonChildNode.optString("Street"); 
       String number  = jsonChildNode.optString("City"); 
       String date_added = jsonChildNode.optString("Longitude"); 
       String lat = jsonChildNode.optString("Lattitude"); 

       System.out.print("Street"+ name + "City" +number+ "Long" + date_added+" Lat" + lat); 

       Toast.makeText(MapsActivity.this, date_added + name + number + lat, Toast.LENGTH_LONG).show(); 

      } 

     } catch (JSONException e) { 
      Toast.makeText(MapsActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
+0

你有數據的JSON數組,並嘗試分析它爲'新的JSONObject(結果)'似乎很明顯 –

回答

-1

你的反應是JSONArray,不是JSONObject,同樣,AccommoAddressJSONObject,不一個JSONArray。因此,您需要將靠近頂部的行更改爲以下行:

JSONArray jsonResponse = null; 

try 
{ 
    jsonResponse = new JSONArray(result); 
    JSONObject jsonMainNode = jsonResponse.optJSONObject("AccommoAddress"); 
+1

請不要回答這樣明顯的愚蠢行爲 –

0

「AccommoAddress」是JSONObject而不是JSONArray。這所以不是..

JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress"); 

試試這個..

/*String Accommo = jsonResponse.getString("AccommoAddress"); 
JSONObject AccomoAddress = new JSONObject(Accommo);*/ 
//simplifying the above code 
JSONObject Accomoaddress = jsonResponse.optJSONObject("AccomoAddress"); 
String name = AccomoAddress.getString("Street"); 
String number = AccomoAddress.getString("City"); 
String date_added = AccomoAddress.getString("Longitude"); 
String lat = AccomoAddress.getString("Lattitude");