json - How can i cast LinkedHashTreeMap to string type? -
json data
{ "id": 8428514522228612, "name": "order acknowledgment", "columns": [ { "id": 7701511984703364, "index": 0, "title": "order#", "type": "text_number", "primary": true }, { "id": 2072012450490244, "index": 1, "title": "order date", "type": "date" } ] }
creating java object , able getid value
myjsonobject1 obj = gson.fromjson(line, myjsonobject1.class); system.out.println(obj.getid());
as columns object array , need loop through , columns key , pair values. need columns id,so trying below code
singlemap nextactioninfomap = new gson().fromjson((string) col,singlemap.class); int column_id = nextactioninfomap.getid(); system.out.println(column_id); class myjsonobject1 { private string id; private string name; private object[] columns; public object[] getcolumns() { return columns; } public void setcolumns(jsonobject[] columns) { this.columns = columns; } public void setid(string id) { this.id = id; } public void setname(string name) { this.name = name; } public string getid() { return id; } public string getname() { return name; }
}
public class singlemap { private int id; private int index; private string title; private string type; private string primary; public int getid() { return id; } public void setid(int id) { this.id = id; } public int getindex() { return index; } public void setindex(int index) { this.index = index; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string gettype() { return type; } public void settype(string type) { this.type = type; } public string getprimary() { return primary; } public void setprimary(string primary) { this.primary = primary; }
}
but getting:
java.lang.classcastexception: com.google.gson.internal.linkedhashtreemap cannot cast java.lang.string
how can resolve issue?
if change columns
property in myjsonobject1
of type singlemap[]
or list<singlemap>
instead of object[]
gson should parsing in 1 go, no need second pass explicitly parse each column.
class myjsonobject1 { private singlemap[] columns; public singlemap[] getcolumns() { return columns; } public void setcolumns(singlemap[] columns) { this.columns = columns; } //... }
Comments
Post a Comment