i develop android application , have deserialize json file. have these classes:
public class medicine { @serializedname("substanta_activa") private list<string> active_substance; @serializedname("produse") private list<product> product; @serializedname("dozaj") private dosage dosage; @serializedname("mentiuni") private list<string> notes; @serializedname("cuvinte_cheie") private list<string> keyword; /* + getters , setters */ }
public class product { @serializedname("denumire_comerciala") private string productname; @serializedname("forme_de_prezentare") private list<string> form; /* + getters , setters */ }
public class dosage { @serializedname("nounascuti") private string newborn; @serializedname("copii") private string child; @serializedname("adulti") private string adult; /* + getters , setters */ }
and have following json file:
[ { "substanta_activa": [ "trimebutinum" ], "produse": [ { "denumire_comerciala": "debridat", "forme_de_prezentare": [ "susp. buvabilă", "susp. 24mg/5ml în flac 250ml", "compr 100mg" ] }, { "denumire_comerciala": "ibutin", "forme_de_prezentare": [ "compr 300mg" ] }, { "denumire_comerciala": "trimebutin", "forme_de_prezentare": [ "compr 100mg" ] }, { "denumire_comerciala": "colperin", "forme_de_prezentare": [ "compr 100mg" ] } ], "dozaj": { "nounascuti": "1ml/kg/zi div 3,", "copii": "1ml/kg/zi div 3, peste 5 ani 3x10ml", "adulti": "3x1-2 compr/zi, 1x300mg/zi sau 3x1-2 lingură/zi" }, "mentiuni": [ "se poate administra de la naștere", "se poate administra amestecat cu apă, lapte", "10ml conține 6g zahăr" ], "cuvinte_cheie": [ "gastro", "colică", "dureri abdominale funcționale", "constipație" ] }, { "substanta_activa": [ "benzydaminum" ], "produse": [ { "denumire_comerciala": "tantum verde comprimate", "forme_de_prezentare": [ "pastile pt supt 3mg" ] }, { "denumire_comerciala": "tantum verde spray", "forme_de_prezentare": [ "spray bucofaringian 0,15%, 0,3%" ] } ], "dozaj": { "nounascuti": "contraindicat", "copii": "2-6 ani: 2-6x1 puf/4kg; >6 sni: 2-6x 4doze sau 3x1 pastila/zi", "adulti": "2-6x 4puf sau 3x1 pastila/zi" }, "mentiuni": [ "se admin. max. 7 zile" ], "cuvinte_cheie": [ "antiseptic, anestezic, antiinflamator, oral, otc" ] } ]
i have tried several ways, gson , without no success. thank in advance.
edit
a little bit more detail: have mainpageactivity, initialize inputstream, set path , call deserialize method jsonparser class:
inputstream = null; string internalstoragepath = getapplicationcontext().getfilesdir().getabsolutepath(); file filetointernalstorage = new file(internalstoragepath + "/medicinelist.json"); try { = new fileinputstream(filetointernalstorage); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } if (is == null) { = getresources().openrawresource(r.raw.gyogyszerek); } jsonparser = new jsonparser(); try { medicines = jsonparser.readjsonstream(getapplicationcontext(), is); //medicines = jsonparser.jsondeserializer(getapplicationcontext(), is); is.close(); } catch (ioexception e) { e.printstacktrace(); }
in jsonparser class, have mentioned have tried several ways deserialize json input. here "traditional" way, android's built-in jsonreader class (sorry, little bit long):
public arraylist readjsonstream(context applicationcontext, inputstream in) throws ioexception { jsonreader reader = new jsonreader(new inputstreamreader(in, "utf-8")); try { return readmedicinearray(reader); } { reader.close(); } } public arraylist readmedicinearray(jsonreader reader) throws ioexception { arraylist medicines = new arraylist(); reader.beginarray(); while (reader.hasnext()) { medicines.add(readmedicine(reader)); } reader.endarray(); return medicines; } public medicine readmedicine(jsonreader reader) throws ioexception { list active_substance = null; list product = null; dosage dosage = null; list notes = null; list keyword = null; reader.beginobject(); while (reader.hasnext()) { string name = reader.nextname(); if (name.equals("substanta_activa")) { active_substance = readactivesubstancearray(reader); } else if (name.equals("produse")) { product = readproductarray(reader); } else if (name.equals("dozaj")) { dosage = readdosage (reader); } else if (name.equals("mentiuni") && reader.peek() != jsontoken.null) { notes= readnotesarray(reader); } else if (name.equals("cuvinte_cheie") && reader.peek() != jsontoken.null) { keyword = readkeywordarray(reader); } else { reader.skipvalue(); } } reader.endobject(); return new medicine(active_substance, product, dosage, notes, keyword); } public list readactivesubstancearray(jsonreader reader) throws ioexception { list active_substance = new arraylist(); reader.beginarray(); while (reader.hasnext()) { active_substance.add(reader.nextstring()); } reader.endarray(); return active_substance; } public list readproductarray(jsonreader reader) throws ioexception { list product = new arraylist(); reader.beginarray(); while (reader.hasnext()) { product.add(readproduct(reader)); } reader.endarray(); return product; } public product readproduct(jsonreader reader) throws ioexception { string productname = null; list form = null; reader.beginobject(); while (reader.hasnext()) { string name = reader.nextname(); if (name.equals("denumire_comerciala")) { productname = reader.nextstring(); } else if (name.equals("forme_de_prezentare")) { form = readformarray(reader); } else { reader.skipvalue(); } } reader.endobject(); return new product(productname, form); } public list readformarray(jsonreader reader) throws ioexception { list form = new arraylist(); reader.beginarray(); while (reader.hasnext()) { form.add(reader.nextstring()); } reader.endarray(); return form; } public dosage readdosage(jsonreader reader) throws ioexception { string newborn= null; string child= null; string adult= null; reader.beginobject(); while (reader.hasnext()) { string name = reader.nextname(); if (name.equals("nounascuti")) { newborn= reader.nextstring(); } else if (name.equals("copii")) { child= reader.nextstring(); } else if (name.equals("adulti")) { adult= reader.nextstring(); } else { reader.skipvalue(); } } reader.endobject(); return new dosage(newborn, child, adult); } public list readnotesarray(jsonreader reader) throws ioexception { list notes= new arraylist(); reader.beginarray(); while (reader.hasnext()) { notes.add(reader.nextstring()); } reader.endarray(); return notes; } public list readkeywordarray(jsonreader reader) throws ioexception { list keyword= new arraylist(); reader.beginarray(); while (reader.hasnext()) { keyword.add(reader.nextstring()); } reader.endarray(); return keyword; }
and here other way gson library:
public arraylist<medicine> jsondeserializer(context contexts, inputstream in) throws ioexception { reader reader = new inputstreamreader(in); arraylist medicineslist = new arraylist(); final gsonbuilder gsonbuilder = new gsonbuilder(); final gson gson = gsonbuilder.create(); medicine[] medicinesarray = new gson().fromjson(reader, medicine[].class); for(int = 0; < medicinesarray.length; ++i){ medicineslist.add(medicinesarray[i]); } return medicineslist; }
none of them works, don't know problem.
//try this
gson gson = new gson(); type listtype = new typetoken<arraylist<medicine>>() {}.gettype(); list<medicine> medlist = gson.fromjson(<your json string>, listtype);