How to convert Arraylist to Json in Java -
i have arraylist, arraylist holds bunch of domain object. it's below showed:
domain [domainid=19, name=a, dnsname=a.com, type=0, flags=0] domain [domainid=20, name=b, dnsname=b.com, type=0, flags=12] domain [domainid=21, name=c, dnsname=c.com, type=0, flags=0] domain [domainid=22, name=d, dnsname=d.com, type=0, flags=0]
my question how convert arraylist
json? data format should be:
{ "param":{ "domain":[ { "domid":19, "name":"a", "dnsname":"a.com", "type":0, "flags": }, ... ] }
not sure if it's need, can use gson library (link) arraylist
json
conversion.
arraylist<string> list = new arraylist<string>(); list.add("str1"); list.add("str2"); list.add("str3"); string json = new gson().tojson(list);
or in case:
arraylist<domain> list = new arraylist<domain>(); list.add(new domain()); list.add(new domain()); list.add(new domain()); string json = new gson().tojson(list);
if reason find more convenient, can iterate through arraylist
, build json
individual domain
objects in list
string tojson(arraylist<domain> list) { gson gson = new gson(); stringbuilder sb = new stringbuilder(); for(domain d : list) { sb.append(gson.tojson(d)); } return sb.tostring(); }
Comments
Post a Comment