java - Two JsonArray`s Comparing missing keys -
i have 2 jsonarrays datas api
api 1
[ { "id":1, "value":270 }, { "id":2, "value":1432493 }, { "id":3, "value":63 }, { "id":5, "value":412 }, { "id":6, "value":371 }, { "id":7, "value":824 }, { "id":9, "value":266 } ]
api 2
[ { "id":1, "name":"name", "description":"description" }, { "id":2, "name":"name", "description":"description" }, { "id":3, "name":"name", "description":"description" }, { "id":4, "name":"name", "description":"description" }, { "id":5, "name":"name", "description":"description" }, { "id":6, "name":"name", "description":"description" }, { "id":7, "name":"name", "description":"description" }, { "id":8, "name":"name", "description":"description" }, { "id":9, "name":"name", "description":"description" } ]
i cambine new array
jsonarray jarray = new jsonarray(api1); jsonarray jarray1 = new jsonarray(api2); for(int i=0;i<jarray1.length();i++) { jsonobject json_data = jarray.getjsonobject(i); jsonobject json_data1 = jarray1.getjsonobject(i); datawallet walletdata = new datawallet(); if (json_data.getstring("id").equals(json_data1.getstring("id"))) { walletdata.wallet_id = json_data.getstring("id"); walletdata.textwalletnumber = json_data.getstring("value"); walletdata.wallet_name = json_data1.getstring("name"); walletdata.wallet_desc = json_data1.getstring("description"); } else { walletdata.wallet_id = json_data1.getstring("id"); walletdata.textwalletnumber = "0"; walletdata.wallet_name = json_data1.getstring("name"); walletdata.wallet_desc = json_data1.getstring("description"); } data.add(walletdata); }
now have problem 1,2,3 work fine 4 problem api1 give no entrys value "0" ad on codes work not.
you need second loop inside the first 1 (jarray1) iterate on jarray. altogether need 2 loops combine 2 arrays. if have huge arrays idea use better algorithms e.g. binary search. makes faster , more optimal o.
here how should like. not have editor right test or there can synax errors. concept right. suggest learn concept of bobble search , binary search. if learn can solve problem in sort.
jsonarray jarray = new jsonarray(api1); jsonarray jarray1 = new jsonarray(api2); for(int i=0;i<jarray1.length();i++) { for(int j=0; j<jarray.lenght();j++){ jsonobject json_data = jarray1.getjsonobject(i); jsonobject json_data1 = jarray.getjsonobject(j); datawallet walletdata = new datawallet(); if (json_data.getstring("id").equals(json_data1.getstring("id"))) { walletdata.wallet_id = json_data.getstring("id"); walletdata.textwalletnumber = json_data.getstring("value"); walletdata.wallet_name = json_data1.getstring("name"); walletdata.wallet_desc = json_data1.getstring("description"); data.add(walletdata); } } }
Comments
Post a Comment