Android json objects to array list in Parcelable class -
i don't know of title accurate, let me explain want. having long jsonobject (sadly it's not array , can't loop through it) many other jsonobjects inside of similar elements (id, name, icon), , when read through element writes value in separate class implemented parcelable. here parcelable class before explain further:
public class itemsinfo implements parcelable { public int itemid; public string itemname, itemicon; @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(itemname); dest.writestring(itemicon); dest.writeint(itemid); } public static final parcelable.creator<itemsinfo > creator = new parcelable.creator<itemsinfo >() { @override public itemsinfo createfromparcel(parcel source) { itemsinfo ei = new itemsinfo(); ei.itemname = source.readstring(); ei.itemicon = source.readstring(); ei.itemid = source.readint(); return ei; } @override public itemsinfo [] newarray(int size) { return new itemsinfo [size]; } }; }
what want everytime reads through jsonobject similar elements, write them arraylist in string itemname, later can access given item index or something, , not have make separate strings , integers every different item, itemname1, itemname2, itemname3..... possible?
you can use droidquery simplify json
parsing. convert jsonobject
key-value mapping, can use this:
list<string> items = new arraylist<string>();//this contain jsonobject strings map<string, ?> data = null; try { jsonobject json;//this references jsonobject data = $.map(json); } catch (throwable t) { log.e("json", "malformed json object"); }
then, loop through each element, this:
if (data != null) { (map.entry<string, ?> entry : data.entryset()) { items.add(entry.value().tostring()); } }
now list
items populated string representation of jsonobejct
s. later, if want parse json, do:
int index = 2;//the index of jsonobject want try { map<string, ?> data = $.map(new jsonobject(items.get(2))); //now iterate map } catch (throwable t) { t.printstacktrace();//something wrong json string }
Comments
Post a Comment