-3
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": "Veteran‎s",
                    "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"
                  }
                ]
        }
        }

And after j son parse Model has contain duplicate id Player[ id=1, _id=531029b207987409620000d6, battingStyle=RHB, bowlingStyle=RAOS, dob=2000-02-28T06: 15: 15.264Z, playingRole=BWL, sname=QadeerButt, fname=Tahir, gallery=[

          ],
          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
                ]
              ]
            ]
          ]
        ]
Shoaib Ahmad
  • 141
  • 1
  • 9

2 Answers2

0

Just check if the Collection (maybe an arraylist?) already has an object with the id before storing a new one.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • i have used GSon lib from parsing json creating model how can i have check this in library gson – Shoaib Ahmad Jul 15 '14 at 10:33
  • http://stackoverflow.com/questions/21556332/make-gson-throw-exception-on-parsing-json-with-duplicated-key – Shivam Verma Jul 15 '14 at 10:36
  • I dont think modify the gson source is a proper solution for him – Emanuel Jul 15 '14 at 10:38
  • already used this solution changing com.google.gson.internal.LinkedTreeMap class but unable to solve @Override public V put(K key, V value) { if (key == null) { throw new NullPointerException("key == null"); } // my edit here if(find(key, false) != null) { throw new IllegalArgumentException("'" + key.toString() + "' is duplicate key for json!"); } Node created = find(key, true); V result = created.value; created.value = value; return result; } – Shoaib Ahmad Jul 15 '14 at 10:38
  • Well, a not so good way would be to just iterate over the collection and remove any duplicates manually but I think there might be a better way to solve this. – Shivam Verma Jul 15 '14 at 10:39
  • public Ground JsonParse(JSONObject jsonObject) { Ground ground = null; try { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); ground = gson.fromJson(jsonObject.getString("grounds") , Ground.class); } catch (Exception e) { e.printStackTrace(); } return ground; } so i iterate jsonObject – Shoaib Ahmad Jul 15 '14 at 10:44
  • What you could also do is to use a Map with the id as the key in place of an ArrayList. – Shivam Verma Jul 15 '14 at 10:48
  • i have used ormlite and GSon in model class as @SerializedName("grounds") @ForeignCollectionField(eager = true, maxEagerLevel = 2) private Collection grounds; – Shoaib Ahmad Jul 15 '14 at 11:13
  • {"players":{"fname": "Tahir","grounds": [{"__v": 26,"_id": "53381c1bf0b","address": "14835 SE 18th Pl, Bellevue, WA 98007, United States","city": "Bellevue","country": "United States","latitude": "","longitude": "","name": "Robinswood Park"},{"__v": 26,"_id": "53381c1bf0bce0bd20000002","address": "14835 SE 18th Pl, Bellevue, WA 98007, United States","city": "Bellevue","country": "United States","latitude": "","longitude": "","name": "Robinswood Park"}],"teams": [{"__v": 30,"_id": "52f86b1d31f3b9b205000a58","category": "Veterans","name": "Totally Cricket","rating": 5,"teamGender": "Men"}]}} – Shoaib Ahmad Jul 15 '14 at 11:30
  • i have duplicate in grounds,teams may be other object also _v duplicate from service so i have all object write this if (!jsonArrayHasItem(secondJson, json.getInt("__v"))) { secondJson.put(json.getJSONObject(i)); } however this is so long if i added new class model we write again code in this funtion for those newly added model – Shoaib Ahmad Jul 15 '14 at 11:34
  • I have updated code on above question and in player model grounds has same id two object insert i have single object in ground because id has same – Shoaib Ahmad Jul 15 '14 at 12:01
0

Yes, but this is what the JSON shows. An JSON Array which contains duplicate JSON Objects.

You may want to valiate while parsing if the JSONObject already exists and copy it in another.

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;
 }

not tested or compiled at all. just pseudocode.

Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • i have used GSon lib from parsing json creating model how can i have check this in library gson – Shoaib Ahmad Jul 15 '14 at 10:36
  • Its easy as hell. Just convert it as JSONArray and modify the JSON Data. Anyway, you should rethink your model if duplicate IDs shouldnt be used. – Emanuel Jul 15 '14 at 10:39
  • public Ground JsonParse(JSONObject jsonObject) { Ground ground = null; try { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); ground = gson.fromJson(jsonObject.getString("grounds") , Ground.class); } catch (Exception e) { e.printStackTrace(); } return ground; } so i have iterate jsonobject – Shoaib Ahmad Jul 15 '14 at 10:44
  • Please try to understand the basics first. We gave you 2 possible solutions, try to understand and to use them – Emanuel Jul 15 '14 at 10:48
  • json.getInt("id") The method getInt(int) in the type JSONArray is not applicable for the arguments (String) – Shoaib Ahmad Jul 15 '14 at 11:04
  • if (jsonArray.has(item)) and The method has(int) is undefined for the type JSONArray – Shoaib Ahmad Jul 15 '14 at 11:08
  • {"players":{"fname": "Tahir","grounds": [{"__v": 26,"_id": "53381c1bf0b","address": "14835 SE 18th Pl, Bellevue, WA 98007, United States","city": "Bellevue","country": "United States","latitude": "","longitude": "","name": "Robinswood Park"},{"__v": 26,"_id": "53381c1bf0bce0bd20000002","address": "14835 SE 18th Pl, Bellevue, WA 98007, United States","city": "Bellevue","country": "United States","latitude": "","longitude": "","name": "Robinswood Park"}],"teams": [{"__v": 30,"_id": "52f86b1d31f3b9b205000a58","category": "Veterans","name": "Totally Cricket","rating": 5,"teamGender": "Men"}]}} – Shoaib Ahmad Jul 15 '14 at 11:30
  • Please update your code and the results you get, else it's hard to read. Ill fix and update onec your post is updated – Emanuel Jul 15 '14 at 11:31
  • i have duplicate in grounds,teams may be other object also _v duplicate from service so i have all object write this if (!jsonArrayHasItem(secondJson, json.getInt("__v"))) { secondJson.put(json.getJSONObject(i)); } however this is so long if i added new class model we write again code in this funtion for those newly added model – Shoaib Ahmad Jul 15 '14 at 11:34
  • And player model game id duplicate insert and i have single object in Ground model – Shoaib Ahmad Jul 15 '14 at 11:59