android - How to insert one more item in a json existing structure with gson? -
the code below generates correctly first structure of json file.
gson = new gsonbuilder().setprettyprinting().create(); auddetheader auddetheader = new auddetheader(); //arraylist<orderdetail> auddetlist = new arraylist<orderdetail>(); map<string, auddet> auddetlist = new hashmap<string, auddet>(); auddet auddet = new auddet(); auddet.setlineid("1"); auddet.setitemnumber("abc"); auddet.setquantity(9); auddet.setprice(10.00); list<string> phones = new arraylist<string>(); phones.add("24530001"); phones.add("24530002"); phones.add("24530003"); auddet.setphones(phones); auddetlist.put("teste 2", auddet); auddetheader.setauddetlist(auddetlist); gson gson = new gsonbuilder().setprettyprinting().create(); string jsonstring = gson.tojson(auddetheader); bufferedwriter bufferedwriter = null; try { file file = new file(environment.getexternalstoragedirectory() + "/download/test/test.json"); if(!file.exists()){ file.createnewfile(); } filewriter filewriter = new filewriter(file); bufferedwriter = new bufferedwriter(filewriter); bufferedwriter.write(jsonstring); } catch (ioexception e) { e.printstacktrace(); } { try { if (bufferedwriter != null){ bufferedwriter.close(); } } catch (ioexception ex) { ex.printstacktrace(); } }
the result of code:
{ "results": { "teste 2": { "itemnumber": "abc", "lineid": "1", "phones": [ "24530001", "24530002", "24530003" ], "price": 10.0, "quantity": 9 } } }
i want add new item. desire stay structure below.
{ "results":{ "teste 2":{ "itemnumber":"abc", "lineid":"1", "phones":[ "24530001", "24530002", "24530003" ], "price":10.0, "quantity":9 }, "teste 3":{ "itemnumber":"def", "lineid":"2", "phones":[ "30303030", "40404040", "505050" ], "price":11.0, "quantity":12 } } }
the auddetheader.class
public class auditoriadetalheheader { @serializedname("results") private map<string, auditoriadetalhe> auditoriadetalhelist; ... }
the auddet.class
public class auditoriadetalhe { string lineid = null; string itemnumber = null; int quantity = 0; double price = null; list<string> phones = new arraylist<string>(); ... }
worked me code!!!
main class
private static file filejson = new file(environment.getexternalstoragedirectory() + "/download/test/test.json"); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.teste_criajson); createjsonstructure(); button btnsave = (button)findviewbyid(r.id.btsave); btnsalvar.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { try { string strfilejson = getstringfromfile(filejson.tostring()); jsonobject jsonobj = new jsonobject(strfilejson); gson gson = new gson(); jsonparser jsonparser = new jsonparser(); string idaud = "10"; auddet ad = new auddet(); ad.setlineid("2"); ad.setitemnumber("def"); ad.setquantity(22); ad.setprice(22.22); list<string> phones = new arraylist<string>(); phones.add("22"); phones.add("22"); phones.add("22"); ad.setphones(phones); string jsonstr = jsonparser.parse(gson.tojson(ad)).tostring(); jsonobject jsonobject = new jsonobject(jsonstr); jsonobj.getjsonobject("results").put(idaud, jsonobject); writejsonfile(filejson, jsonobj.tostring()); } catch (exception e1) { e1.printstacktrace(); } } });
if not exists json file, create basic structure insert itens.
public static void createjsonstructure(){ if(!filejson.exists()){ try { filejson.createnewfile(); string jsonstring = "{\"results\":{}}"; writejsonfile(filejson, jsonstring); } catch (ioexception e) { e.printstacktrace(); } } }
open json file string format, , prepare insert new item:
public static string getstringfromfile (string filepath) throws exception { file fl = new file(filepath); fileinputstream fin = new fileinputstream(fl); string ret = convertstreamtostring(fin); //make sure close streams. fin.close(); return ret; } public static string convertstreamtostring(inputstream is) throws exception { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line).append("\n"); } return sb.tostring(); }
writes json file exists:
public static void writejsonfile(file file, string json) { bufferedwriter bufferedwriter = null; try { if(!file.exists()){ file.createnewfile(); } filewriter filewriter = new filewriter(file); bufferedwriter = new bufferedwriter(filewriter); bufferedwriter.write(json); } catch (ioexception e) { e.printstacktrace(); } { try { if (bufferedwriter != null){ bufferedwriter.close(); } } catch (ioexception ex) { ex.printstacktrace(); } } }
Comments
Post a Comment