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解析一些困難
我會考慮使用Gson進行序列化和反序列化。它更清潔,更簡單,並且可以輕鬆捕捉錯誤。 –