2017-06-20 27 views
-1

Ich habe eine JSON-Datei, die ich analysieren möchte, aber nicht weiß, wie Sie richtig darauf zugreifen.Android Studio: JSON Parsing

Es beginnt nicht mit einer Objektklammer "{" und danach ein Name wie z. "actors:" "[" ....]}

wo ich einfach ein JSONObject jObj = neues JSONObject (Daten) erstellen würde; JSONArray jArray = jObj.getJSONArray ("Akteure");

Mine sieht eher wie dieses

[ 
{ 
"type": "fuel", 
"name": "Aral", 
"address": "Somestreet 65", 
"lat": 49.8848387, 
"lon": 8.6520691 }, 
{ 
"type": "amenity", 
"name": "Centralstation", 
"address": "Centralstreet 20", 
"lat": 49.8725, 
"lon": 8.628889, 
"icon": "somepicture.jpg" }, 
] 

ich etwas versucht, wie

try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(params[0]); 
      HttpResponse response = client.execute(post); 

      int status = response.getStatusLine().getStatusCode(); 

      if(status == 200){ 
       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity); 

       JSONArray jsonArray = new JSONArray(data); 
       //JSONObject jsonObject = new JSONObject(data); 
       for(int i=0; i< jsonArray.length();i++){ 
        Locations location = new Locations(); 
        JSONObject jRealObject = jsonArray.getJSONObject(i); 
        location.setName(jRealObject.getString("type")); 
        location.setName(jRealObject.getString("name")); 
        location.setName(jRealObject.getString("address")); 
        location.setName(jRealObject.getString("lat")); 
        location.setName(jRealObject.getString("lon")); 
        //location.setImage(jRealObject.getString("icon")); 
        locationList.add(location); 
       } 
       return true; 
      } 

     }catch (ClientProtocolException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }catch (JSONException e){ 
      e.printStackTrace(); 
     } 
     return false; 
    } 

Aber es ist ein Fehler, während es das Parsen Ich denke, es hat etwas mit „JSONArray jsonArray = new JSONArray zu tun hat („“);

können Sie mich oder Punkt in einer Richtung helfen, wo ich meine Fehler finden konnte

+0

JSON-Array initialisieren als: JSONArray jasnArray = new JSONArray(); – Akshay

+0

funktioniert nicht für mich –

Antwort

0
JSONArray jarray=new JSONArray(data); 
for (int i=0;i<=jarray.length();i++) 
    { 
     JSONObject obj1=jarray.getJSONObject(i); 
     String address=obj1.getString("type"); 
     String caseno=obj1.getString("name"); 
     String casetype=obj1.getString("address"); 
    } 
+0

kann ich immer noch mit meinem LocationList Array arbeiten, so dass ich keine String-Adresse = obj1.getString ("Typ") tun muss? Ich versuchte Ihr Beispiel, aber es funktioniert immer noch nicht –

0

try this ....

  JSONArray jsonArray = new JSONArray (data); 
        for(int i=0; i< jsonArray.length();i++){ 
         Locations location = new Locations(); 
         JSONObject jRealObject = jsonArray.getJSONObject(i); 
         location.setName(jRealObject.getString("type")); 
         location.setName(jRealObject.getString("name")); 
         location.setName(jRealObject.getString("address")); 
         location.setName(jRealObject.getString("lat")); 
         location.setName(jRealObject.getString("lon")); 
         locationList.add(location); 
        } 

-wenn Sie einen differnt Schlüssel bekommen verwenden Iterator Schlüssel zu bekommen wie

Iterator<String> iter = json.keys(); 
while (iter.hasNext()) { 
    String key = iter.next(); 
    try { 
     Object value = json.get(key); 
    } catch (JSONException e) { 
     // Something went wrong! 
    } 
} 
+0

Danke für Ihre Antwort, aber es funktioniert immer noch nicht –