2016-08-02 5 views
-1

Kann mir jemand sagen, wie man Daten von Json, die mehrere Array haben. Ich bin neu auf AndroidWie bekomme ich Daten von JSON, es hat mehrere JSON-Array

Das ist mein Json Code:

[ 
    { 
     "id": "17", 
     "name": "dummy1", 
     "com": [ 
      { 
       "user": "ijas", 
       "comment": "thanQ test2" 
      }, 
      { 
       "user": "sam", 
       "comment": "test1" 
      }, 
      { 
       "user": "sam", 
       "comment": "thanQ test1" 
      }, 
      { 
       "user": "ijas", 
       "comment": "test2" 
      }, 
      { 
       "user": "deepak", 
       "comment": "thanQ test3" 
      } 
     ] 
    } 
] 
+0

Sie könnten Gson oder Jackson verwenden. Suche auf Google, es ist ziemlich einfach zu verwenden –

+0

okay, können Sie mir den Code senden, den ich benötige, um die Daten zu holen – Sam

+0

Mit diesen Bibliotheken müssen Sie nicht manuell abholen. Sie können direkt mit Anmerkungen in Ihrem Datenmodell arbeiten. Sie können Beispiele überall im Internet finden https://kylewbanks.com/blog/Tutorial-Android-Parsing-JSON-mit-GSON –

Antwort

0

Hier ist Ihre json Version mit Gson analysiert.

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class Com { 

@SerializedName("user") 
@Expose 
private String user; 
@SerializedName("comment") 
@Expose 
private String comment; 

/** 
* 
* @return 
* The user 
*/ 
public String getUser() { 
return user; 
} 

/** 
* 
* @param user 
* The user 
*/ 
public void setUser(String user) { 
this.user = user; 
} 

/** 
* 
* @return 
* The comment 
*/ 
public String getComment() { 
return comment; 
} 

/** 
* 
* @param comment 
* The comment 
*/ 
public void setComment(String comment) { 
this.comment = comment; 
} 

} 
-----------------------------------com.example.Example.java----------------------------------- 

package com.example; 

import java.util.ArrayList; 
import java.util.List; 
import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class Example { 

@SerializedName("id") 
@Expose 
private String id; 
@SerializedName("name") 
@Expose 
private String name; 
@SerializedName("com") 
@Expose 
private List<Com> com = new ArrayList<Com>(); 

/** 
* 
* @return 
* The id 
*/ 
public String getId() { 
return id; 
} 

/** 
* 
* @param id 
* The id 
*/ 
public void setId(String id) { 
this.id = id; 
} 

/** 
* 
* @return 
* The name 
*/ 
public String getName() { 
return name; 
} 

/** 
* 
* @param name 
* The name 
*/ 
public void setName(String name) { 
this.name = name; 
} 

/** 
* 
* @return 
* The com 
*/ 
public List<Com> getCom() { 
return com; 
} 

/** 
* 
* @param com 
* The com 
*/ 
public void setCom(List<Com> com) { 
this.com = com; 
} 

} 

können Sie über diesen Link: http://www.jsonschema2pojo.org/

+0

danke für Ihre Hilfe – Sam

0

Es ist nicht die Anzahl der JSON-Arrays oder JSON-Objekte in einem einzigen JSON-Objekt Materie. Da es immer einen Schlüssel auf der linken Seite des Werts zugeordnet hat. In der Probe Sie ein JSON-Array zur Verfügung gestellt hat com, genannt Sie können analysieren, dass die folgende Verwendung

JSONObject resultJSON=new JSONObject(responseString); 
    try { 
     JSONArray comJSONArray=resultJSON.getJSONArray("com"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

Besser Sie folgen können, wie andere beantwortet. Verwenden Sie Gson mit der POJO Klasse, so dass Sie müssen alle diese Codes nicht schreiben die json

0

Aus meiner Sicht zu analysieren i Code unten bin Entsendung Abrufen von Daten aus JSON-String

private void GetJson(String jsonString) { 
     try { 
      JSONArray array = new JSONArray(jsonString); 
      { 
       for (int i = 0; i < array.length(); i++) { 
        JSONObject jsonObject = array.getJSONObject(i); 

        int id = jsonObject.getInt("id"); 
        String name = jsonObject.getString("name"); 
        Log.e("ID", String.valueOf(id)); 
        Log.e("NAME", name); 

        String jsonCom = jsonObject.getString("com"); 

        JSONArray jsonArray = new JSONArray(jsonCom); 
        for (int j = 0; j < jsonArray.length(); j++) { 

         JSONObject objCom = jsonArray.getJSONObject(j); 
         String user = objCom.getString("user"); 
         String comment = objCom.getString("comment"); 

         Log.e("USER", user); 
         Log.e("COMMENT", comment); 

        } 
       } 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

Wenn Sie die ASYNK-Task zum Abrufen von Daten aus WebService verwendet haben, rufen Sie GetJson ("Your json") in der onPostMethod() -Methode auf und wenn Sie Volly zum Abrufen von Daten verwendet haben, müssen Sie unseren GetJson ("json string") in

aufrufen
@Override 
public void onResponse(String response) { 

    GetJson(response); 

} 
+0

Vielen Dank für Ihre Antwort, ihre Arbeit für mich danke – Sam

+0

wenn Sie sind glücklich mit meiner Antwort, als Sie die richtige Antwort geben können oder stimmen Sie mich ab, so dass jede andere Phase dieses Problem nächstes Mal anderen helfen wird. – Kintanpatel