json - GSON not returning key name, only value (Java) -
i'm trying format string data json using gson api, shown in returnjson() method:
import com.google.gson.*; import com.google.gson.annotations.serializedname; public class hukd { @serializedname("title") public string title; @serializedname("deal url") public string dealurl; @serializedname("product url") public string producturl; @serializedname("image url") public string imgurl; @serializedname("description") public string description; @serializedname("temperature") public string temperature; @serializedname("ean") public string ean; @serializedname("price") public string price; @serializedname("amazon price") public string amazonprice; @serializedname("price difference") public string pricedifference; @serializedname("amazon url") public string amazonurl; public hukd(string title, string dealurl, string producturl, string imgurl, string description, string temperature, string ean, string price, string amazonprice, string pricedifference, string amazonurl) { this.title = title; this.dealurl = dealurl; this.producturl = producturl; this.imgurl = imgurl; this.description = description; this.temperature = temperature; this.ean = ean; this.price = price; this.amazonprice = amazonprice; this.pricedifference = pricedifference; this.amazonurl = amazonurl; } public string returnjson(){ system.out.println("********testing objects*************"); string[] jsonbuilder = new string []{title, dealurl, producturl, imgurl, description, temperature, ean, price, amazonprice, pricedifference, amazonurl}; gson gson = new gsonbuilder().setprettyprinting().setfieldnamingpolicy(fieldnamingpolicy.upper_camel_case).create(); string json = gson.tojson(jsonbuilder ); return json; }
however, gson return value, not key:
[ "philips shm3560/10 on ear headphones £8.99 argos", "http://www.hotukdeals.com/deals/philips-shm3560-10-headphones-8-99-from-argos-2390246?aui\u003d1063", "http://www.hotukdeals.com/visit?m\u003d5\u0026q\u003d2390246", "http://static.hotukdeals.com/images/threads/2390246_1.jpg", "argos cat no- 108/7390\nhttp://www.argos.co.uk/webapp/wcs/stores/servlet/searchmobile?storeid\u003d10151\u0026catalogid\u003d25051\u0026langid\u003d110\u0026searchterm\u003d108%2f7390", "171°", "8712581626211", "£8.99", "£12.59", "argos cheaper amazon £3.60", "http://www.amazon.co.uk/philips-shm3560-10-pc-headset-shm3560/dp/b008fse6eu%3fsubscriptionid%3dakiailn2tpm667mbmjaq%26tag%3dgithubcomthis-21%26linkcode%3dxm2%26camp%3d2025%26creative%3d165953%26creativeasin%3db008fse6eu" ]
i've tried around adding serializedname annotation suggested in "json field naming support" section of api documentation here, i'm still not having luck. ideally, i'd json formatted so:
"title":"philips shm3560/10 on ear headphones £8.99 argos"
(and on...), i.e. key name printed specified in @serializedname annotation in field declaration.
thanks!
you not serializing object, array of strings see proper result of serialization. if want serialize object must use this:
public string returnjson(){ gson gson = new gsonbuilder().setprettyprinting().setfieldnamingpolicy(fieldnamingpolicy.upper_camel_case).create(); string json = gson.tojson(this); return json; }
Comments
Post a Comment