Json from server
{
"status": 1,
"players": {
"__v": 5,
"_id": "52f719eb31f3b9b20500000c",
"battingStyle": "Left Hand Batsman",
"bowlingStyle": "Facing Batsman",
"dob": "2000-02-09T06:01:49.144Z",
"fname": "Muhammad",
"playingRole": "Bowler",
"sname": "Sami",
"teams": [
{
"__v": 23,
"_id": "52f715b431f3b9b205000004",
"category": "Veterans",
"name": "Golden Eagle",
"rating": 5,
"teamGender": "Men"
},
{
"__v": 17,
"_id": "52f715df31f3b9b205000005",
"category": "Veterans",
"name": "Choudhry Sports",
"rating": 5,
"teamGender": "Men"
}
],
"grounds": [
{
"__v": 2,
"_id": "53381bb5f0bce0bd20000001",
"address": "6046 W Lake Sammamish Way Northeast, Redmond, WA 98052, United States",
"city": "Redmond",
"country": "United States",
"latitude": "",
"longitude": "",
"name": "Marymoor Park"
},
{
"__v": 2,
"_id": "53381bb5f0bce0bd20000001",
"address": "6046 W Lake Sammamish Way Northeast, Redmond, WA 98052, United States",
"city": "Redmond",
"country": "United States",
"latitude": "",
"longitude": "",
"name": "Marymoor Park"
}
]
}
}
并且在j子解析模型之后包含重复的id 播放器[ ID = 1, _id = 531029b207987409620000d6, battingStyle = RHB, bowlingStyle = RAOS, dob = 2000-02-28T06:15:15.264Z, playingRole = BWL, SNAME = QadeerButt, FNAME =塔希尔, 廊= [
],
games=[
Game[
id=4,
gameType=T20,
name=TotallyCricketvsLahoreTiger,
totalOvers=20,
dateStarted=2014-06-18T19: 00: 00.000Z,
dateEnded=2014-06-18T19: 00: 00.000Z,
innings=[
],
grounds=[
Ground[
id=26,
address=14835SE18thPl,
Bellevue,
WA98007,
UnitedStates,
city=Bellevue,
country=UnitedStates,
name=RobinswoodPark
],
Ground[
id=26,
address=14835SE18thPl,
Bellevue,
WA98007,
UnitedStates,
city=Bellevue,
country=UnitedStates,
name=RobinswoodPark
]
],
teams=[
Team[
id=30,
name=TotallyCricket,
category=Veterans,
teamGender=Men,
rating=5
],
Team[
id=19,
name=LahoreTiger,
category=Under19,
teamGender=Men,
rating=3
]
]
]
]
]
答案 0 :(得分:0)
只需检查Collection(可能是一个arraylist?)是否已经有一个带有id的对象,然后再存储一个新对象。
答案 1 :(得分:0)
是的,但这是JSON显示的内容。包含重复JSON对象的JSON数组。
如果JSONObject已经存在,您可能希望在解析时进行验证,并将其复制到另一个。
GSON gson = new GSON();
JSONArray json = new JSONArray(gson.toJson(yourobject));
JSONArray secondJson = new JSONArray();
for (int i = 0; i <= json.length(); i++) {
if (!jsonArrayHasItem(secondJson, json.getInt("id")) {
secondJson.put(jsonArray.getJsonObject(i));
}
}
public boolean jsonArrayHasItem(JSONArray jsonArray, int item) {
for (int i = 0; i <= jsonArray.length(); i++) {
if (jsonArray.getJsonObject(i).has(item)) return true;
}
return false;
}
根本没有测试或编译。只是伪代码。