2016-07-26 8 views
-1

Ich habe Probleme; Ich kann die innere Zeichenfolge von einem JsonObject nicht lesen. Es besagt, dass JsonArray nicht in JsonObject konvertiert werden kann.JSON-String von einem JsonObject in Android lesen

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"}] 

Hier ist mein JsonArray. Ich versuche, von AccommoAddress zu lesen, aber ich habe den Fehler unten:

[{"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"}] 

Hier ist mein Code

@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

Sie haben ein JSON-Array in Daten und Sie versuchen, es zu analysieren, wie 'neue JSONObject (Ergebnis)' scheint ziemlich offensichtlich –

Antwort

-1

Ihre Antwort ein JSONArray, kein JSONObject, ähnlich, AccommoAddress ein JSONObject ist, nicht a JSONArray. Sie müssen also die Linien ändern nahe der Spitze auf die folgenden:

JSONArray jsonResponse = null; 

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

Ziehen Sie bitte an Antworte nicht auf solche offensichtlichen Betrüger –

0

„AccommoAddress“ ist ein JSONObject kein JSONArray. Anstatt also diese ..

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

die Sie interessieren ..

/*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"); 
Verwandte Themen