2016-12-05 58 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);