java - String to Array using Json -
i have array of json elements string, problem how convert them , object array using gson. found methods on site non of them seem work string.
["2": {"id": 2, "name": "cannonball", "sp": 5, "overall_average": 194, "buy_average": 193, "members": true, "sell_average": 193}, "6": {"id": 6, "name": "cannon base", "sp": 187500, "overall_average": 188110, "buy_average": 184547, "members": true, "sell_average": 185735}, "12289": {"id": 12289, "name": "mithril platelegs (t)", "sp": 2600, "overall_average": 0, "buy_average": 3000, "members": false, "sell_average": 3000}, "8": {"id": 8, "name": "cannon stand", "sp": 187500, "overall_average": 198445, "buy_average": 189001, "members": true, "sell_average": 190889}, "10": {"id": 10, "name": "cannon barrels", "sp": 187500, "overall_average": 194418, "buy_average": 185164, "members": true, "sell_average": 185935}, "12": {"id": 12, "name": "cannon furnace", "sp": 187500, "overall_average": 188000, "buy_average": 186524, "members": true, "sell_average": 186637}, "4099": {"id": 4099, "name": "mystic hat (dark)", "sp": 15000, "overall_average": 9758, "buy_average": 9229, "members": true, "sell_average": 9528}]
i need convert data java object.
public class osbuddyitem { private final int id; private final string name; private final int sellprice; private final int buyprice; private final int averageprice; private final int storeprice; private final boolean members; public osbuddyitem(int id, string name, int sellprice, int buyprice, int averageprice, int storeprice, boolean members){ this.id = id; this.name = name; this.sellprice = sellprice; this.buyprice = buyprice; this.averageprice = averageprice; this.storeprice = storeprice; this.members = members; } public int getid() { return id; } public string getname() { return name; } public int getsellprice() { return sellprice; } public int getbuyprice() { return buyprice; } public int getaverageprice() { return averageprice; } public int getstoreprice() { return storeprice; } public boolean ismembers() { return members; } }
this tried:
gson gson = new gson(); jsonparser parser = new jsonparser(); jsonelement tradeelement = parser.parse(data); jsonarray itemelements = tradeelement.getasjsonarray(); osbuddyitem[] items = gson.fromjson(itemelements,osbuddyitem[].class); for(osbuddyitem item : items){ system.out.println(item.getname()); }
can please tell me how convert string using gson?
your json string not valid.you can validate json string [http://jsoneditoronline.org/][1]
suppose if json string in form :-
[{"1": {"id": 2, "name": "cannonball", "sp": 5, "overall_average": 194, "buy_average": 193, "members": true, "sell_average": 193}}]
suppose model name example.then model of json :-
package com.example;
import com.google.gson.annotations.serializedname;
public class example {
@serializedname("id") private integer id; @serializedname("name") private string name; @serializedname("sp") private integer sp; @serializedname("overall_average") private integer overallaverage; @serializedname("buy_average") private integer buyaverage; @serializedname("members") private boolean members; @serializedname("sell_average") private integer sellaverage; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getsp() { return sp; } public void setsp(integer sp) { this.sp = sp; } public integer getoverallaverage() { return overallaverage; } public void setoverallaverage(integer overallaverage) { this.overallaverage = overallaverage; } public integer getbuyaverage() { return buyaverage; } public void setbuyaverage(integer buyaverage) { this.buyaverage = buyaverage; } public boolean getmembers() { return members; } public void setmembers(boolean members) { this.members = members; } public integer getsellaverage() { return sellaverage; } public void setsellaverage(integer sellaverage) { this.sellaverage = sellaverage; }
}
and using gson ,you can convert below. [note :typetoken class used load json string custom object]
list<example> mylist = new gson().fromjson(br, new typetoken<list<jsonlog>>(){}.gettype());
Comments
Post a Comment