2016-12-05 57 views
0
My json response is below 

    { 
     "groups":[ 
      { 
      "storeId":"440f0991-9ac5-41b9-9e84-d3b2a2d27c13", 
      "name":"oty", 
      "description":"ga", 
      "image":null, 
      "bookCount":2, 
      "members":[ 
       { 
        "bookId":"9b765d0f-3d6f-4fc1-a4a7-af807c39a004", 
        "bookName":"Anantha" 
       }, 
       { 
        "bookId":"f8616ab1-eeb3-403b-8182-b42e39f4b948", 
        "bookName":"lok" 
       } 
      ] 
      } 
     ] 
    } 

My code for parsing json 

    JSONObject jsonObject = new JSONObject(message); 
      JSONArray jsonArray = jsonObject.getJSONArray("groups"); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       String storeid = jsonArray.getJSONObject(i).getString("storeId"); 
       String name = jsonArray.getJSONObject(i).getString("name"); 
       String description =jsonArray.getJSONObject(i).getString("description"); 
       String bookCount = jsonArray.getJSONObject(i).getString("bookCount"); 
       JSONArray memberJsonArray = jsonArray.getJSONObject(i).getJSONArray("members"); 
       for (int j = 0; j < memberJsonArray.length(); j++) { 
        String bookId = memberJsonArray.getJSONObject(j).getString("bookId"); 
        String bookName = memberJsonArray.getJSONObject(j).getString("bookName") 
        GroupsDto groupDtoData = new GroupsDto(); 
        groupDtoData.setGroupName(name); 
        groupDtoData.setGroupServerId(storeId); 


        groupDtoData.setbookname(bookName); 
        groupDtoData.setbookid(bookId); 

        groupDto.add(groupDtoData); 
        db.addGroups(groupDtoData); 
       } 
      } 
but I got result as if book name is twice then the storeId is also twice.simillarly increse in no of book name,store id also increse.so it reflect on the list view having duplicate store name. 
I/System.out: storeId 440f0991-9ac5-41b9-9e84-d3b2a2d27c13 
I/System.out: bookName Ananta 
I/System.out: storeId groupid440f0991-9ac5-41b9-9e84-d3b2a2d27c13 
I/System.out: bookname Lok 

但我想用一個STOREID有兩名本書的名字,但店鋪標識也越來越兩次 請給一些想法如何分析這種類型json.I都面臨這個問題的JSON解析但我想用一個storeId有兩個書名,但商店ID也越來越兩次面臨JSON解析一些困難

+0

我會考慮使用Gson進行序列化和反序列化。它更清潔,更簡單,並且可以輕鬆捕捉錯誤。 –

回答

1

使用Gson序列化和反序列化。 1.創建一個模型類,您的情況如下所示。

public class MyResponse { 

@SerializedName("groups") 
private List<Groups> groupsList; 

public List<Groups> getGroupsList() { 
    return groupsList; 
} 

public class Groups { 

    @SerializedName("storeId") 
    private String storeId; 

    @SerializedName("name") 
    private String name; 

    @SerializedName("description") 
    private String description; 

    @SerializedName("bookCount") 
    private String bookCount; 

    @SerializedName("members") 
    private List<Members> members; 

    public String getStoreId() { 
     return storeId; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public String getBookCount() { 
     return bookCount; 
    } 

    public List<Members> getMembers() { 
     return members; 
    } 

    public class Members { 

     @SerializedName("bookId") 
     private String bookId; 

     @SerializedName("bookName") 
     private String bookName; 

     public String getBookName() { 
      return bookName; 
     } 

     public String getBookId() { 
      return bookId; 
     } 
    } 
    } 
} 
  • 確保你對你的gradle這個文件中添加GSON lib如果使用機器人工作室

    依賴{ 編譯「com.google.code.gson:GSON: 2.6.2' }

  • 使用響應字符串,因爲您的情況是消息對象並將其轉換爲響應對象,如下所示。 Gson gson = new GsonBuilder()。create();

    MyResponse myResponse = gson.fromJson(message,MyResponse.class);