Find unique data from json using android -
how fetch unique results json response using andorid.
hi, need fetch data json webservice, have read data's in json. need store unique value json.
is possible fetch unique value json?
first, json malformated:
{ "lastyear": [ { "order": "96 36 08", "year": "2015", "text": "no" }, { "order": "03 59 03", "year": "2014", "text": "no" }, { "order": "16 50 58", "year": "2014", "text": "no" } ]}
to parse array, need implement this
import org.json.*; import java.util.arraylist; public class myobject { private arraylist<lastyear> lastyear; public myobject () { } public myobject (jsonobject json) { this.lastyear = new arraylist<lastyear>(); jsonarray arraylastyear = json.optjsonarray("lastyear"); if (null != arraylastyear) { int lastyearlength = arraylastyear.length(); (int = 0; < lastyearlength; i++) { jsonobject item = arraylastyear.optjsonobject(i); if (null != item) { this.lastyear.add(new lastyear(item)); } } } else { jsonobject item = json.optjsonobject("lastyear"); if (null != item) { this.lastyear.add(new lastyear(item)); } } } public arraylist<lastyear> getlastyear() { return this.lastyear; } public void setlastyear(arraylist<lastyear> lastyear) { this.lastyear = lastyear; } }
and sub-object:
import org.json.*; public class lastyear { private string order; private string year; private string text; public lastyear () { } public lastyear (jsonobject json) { this.order = json.optstring("order"); this.year = json.optstring("year"); this.text = json.optstring("text"); } public string getorder() { return this.order; } public void setorder(string order) { this.order = order; } public string getyear() { return this.year; } public void setyear(string year) { this.year = year; } public string gettext() { return this.text; } public void settext(string text) { this.text = text; } }
how use it:
myobject object = new myobject(new jsonobject("your_json")); //verifiy if object.getlastyear != null int sizelist = object.getlastyear().size; for(int = 0; < sizelist;i++){ lastyear lastyear = object.getlastyear().get(i) log.e("app",lastyear.getyear); }
to have unique value, can use :
private static string[] arrremove(string[] strarray) { set<string> set = new hashset<string>(); set.addall((list<string>) arrays.aslist(strarray)); return (string[]) set.toarray(new string[set.size()]); }
Comments
Post a Comment