spring - How would I parse this JSON, looks like an array but no "[]"s -
i'm using springboot resttemplate parse json. resources i'm looking @ deal arrays, understand arrays need brackets ([]).
i have json here (https://rsbuddy.com/exchange/summary.json)
(short example in case link goes down)
{"2": {"overall_average": 216, "sp": 5, "members": true, "buy_average": 215, "sell_average": 215, "name": "cannonball", "id": 2}, "6": {"overall_average": 173518, "sp": 187500, "members": true, "buy_average": 190176, "sell_average": 189343, "name": "cannon base", "id": 6}, ... , "12287": {"overall_average": 0, "sp": 5200, "members": false, "buy_average": 3234, "sell_average": 3234, "name": "mithril platebody (t)", "id": 12287}}
that feels should array because it's list, doesn't have brackets, , can't parsed following code:
public list<itemsummarycontainer> consumeitems() { responseentity<list<itemsummarycontainer>> itemsummaryresponse = resttemplate.exchange(url, httpmethod.get, null, new parameterizedtypereference<list<itemsummarycontainer>>() { }); return itemsummaryresponse.getbody(); }
itemsummarycontainer class
@jsonignoreproperties(ignoreunknown = true) public class itemsummarycontainer { private int id; private itemsummary itemsummary; public int getid() { return id; } public void setid(int id) { this.id = id; } public itemsummary getitemsummary() { return itemsummary; } public void setitemsummary(itemsummary itemsummary) { this.itemsummary = itemsummary; } }
itemsummary class
@jsonignoreproperties(ignoreunknown = true) public class itemsummary { private integer id; private string name; private integer members; public itemsummary() { } public itemsummary(integer id, string name, integer members) { super(); this.id = id; this.name = name; this.members = members; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public int getmembers() { return members; } public void setmembers(int members) { this.members = members; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; itemsummary other = (itemsummary) obj; if (id != other.id) return false; if (members != other.members) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
stacktrace
java.lang.illegalstateexception: failed execute commandlinerunner @ org.springframework.boot.springapplication.callrunner(springapplication.java:801) [spring-boot-1.4.0.release.jar:1.4.0.release] @ org.springframework.boot.springapplication.callrunners(springapplication.java:782) [spring-boot-1.4.0.release.jar:1.4.0.release] @ org.springframework.boot.springapplication.afterrefresh(springapplication.java:769) [spring-boot-1.4.0.release.jar:1.4.0.release] @ org.springframework.boot.springapplication.run(springapplication.java:314) [spring-boot-1.4.0.release.jar:1.4.0.release] @ org.springframework.boot.springapplication.run(springapplication.java:1185) [spring-boot-1.4.0.release.jar:1.4.0.release] @ org.springframework.boot.springapplication.run(springapplication.java:1174) [spring-boot-1.4.0.release.jar:1.4.0.release] @ com.tjwhalen.game.application.main(application.java:50) [classes/:na] caused by: org.springframework.http.converter.httpmessagenotreadableexception: not read document: can not deserialize instance of java.util.arraylist out of start_object token @ [source: java.io.pushbackinputstream@55f8669d; line: 1, column: 1]; nested exception com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of java.util.arraylist out of start_object token @ [source: java.io.pushbackinputstream@55f8669d; line: 1, column: 1] @ org.springframework.http.converter.json.abstractjackson2httpmessageconverter.readjavatype(abstractjackson2httpmessageconverter.java:228) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.http.converter.json.abstractjackson2httpmessageconverter.read(abstractjackson2httpmessageconverter.java:213) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.httpmessageconverterextractor.extractdata(httpmessageconverterextractor.java:95) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.resttemplate$responseentityresponseextractor.extractdata(resttemplate.java:884) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.resttemplate$responseentityresponseextractor.extractdata(resttemplate.java:868) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:622) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.resttemplate.execute(resttemplate.java:580) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ org.springframework.web.client.resttemplate.exchange(resttemplate.java:526) ~[spring-web-4.3.2.release.jar:4.3.2.release] @ com.tjwhalen.game.service.dao.itemsummaryurlconsumer.consumeitems(itemsummaryurlconsumer.java:25) ~[classes/:na] @ com.tjwhalen.game.service.impl.itemsummaryrestserviceimpl.getitems(itemsummaryrestserviceimpl.java:25) ~[classes/:na] @ com.tjwhalen.game.loader.loaditems.load(loaditems.java:38) ~[classes/:na] @ com.tjwhalen.game.loader.loaderrunner.execute(loaderrunner.java:26) ~[classes/:na] @ com.tjwhalen.game.application$appconfig.run(application.java:78) ~[classes/:na] @ org.springframework.boot.springapplication.callrunner(springapplication.java:798) [spring-boot-1.4.0.release.jar:1.4.0.release] ... 6 common frames omitted caused by: com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of java.util.arraylist out of start_object token @ [source: java.io.pushbackinputstream@55f8669d; line: 1, column: 1] @ com.fasterxml.jackson.databind.jsonmappingexception.from(jsonmappingexception.java:261) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deserializationcontext.reportmappingexception(deserializationcontext.java:1233) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deserializationcontext.handleunexpectedtoken(deserializationcontext.java:1121) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deserializationcontext.handleunexpectedtoken(deserializationcontext.java:1074) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.handlenonarray(collectiondeserializer.java:328) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:259) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:249) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.deser.std.collectiondeserializer.deserialize(collectiondeserializer.java:26) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.objectmapper._readmapandclose(objectmapper.java:3789) ~[jackson-databind-2.8.1.jar:2.8.1] @ com.fasterxml.jackson.databind.objectmapper.readvalue(objectmapper.java:2913) ~[jackson-databind-2.8.1.jar:2.8.1] @ org.springframework.http.converter.json.abstractjackson2httpmessageconverter.readjavatype(abstractjackson2httpmessageconverter.java:225) ~[spring-web-4.3.2.release.jar:4.3.2.release] ... 19 common frames omitted
please advise, or ask needed clarifying questions,
thanks
the json trying parse valid. it's not array, it's still valid:
{ "2": { "overall_average": 211, "buy_average": 208, "members": true, "id": 2, "name": "cannonball", "sell_average": 210, "sp": 5 }, "6": { "overall_average": 0, "buy_average": 0, "members": true, "id": 6, "name": "cannon base", "sell_average": 0, "sp": 187500 }, "12289": { "overall_average": 9999, "buy_average": 0, "members": false, "id": 12289, "name": "mithril platelegs (t)", "sell_average": 9999, "sp": 2600 }, ... }
the itemsummary
class defined following:
@jsonignoreproperties(ignoreunknown = true) public class itemsummary { private string id; private string name; private boolean members; // getters , setters omitted }
and whole json parsed map<string, itemsummary>
. see details below.
parsing json map<string, itemsummary>
with spring rest template, use:
resttemplate resttemplate = new resttemplate(); responseentity<map<string, itemsummary>> response = resttemplate.exchange( "https://rsbuddy.com/exchange/summary.json", httpmethod.get, null, new parameterizedtypereference<map<string, itemsummary>>() {}); map<string, itemsummary> map = response.getbody();
with jackson's objectmapper
, have:
objectmapper mapper = new objectmapper(); map<string, itemsummary> map = mapper.readvalue(new url("https://rsbuddy.com/exchange/summary.json"), new typereference<map<string, itemsummary>>() {});
wrapping map<string, itemsummary>
class
if need wrapper class around map<string, itemsummary>
, define following:
@jsonignoreproperties(ignoreunknown = true) public class itemsummarycontainer { private map<string, itemsummary> items; @jsoncreator public itemsummarycontainer(map<string, itemsummary> items) { this.items = items; } // getters , setters omitted }
with spring rest template, use:
resttemplate resttemplate = new resttemplate(); responseentity<itemsummarycontainer> response = resttemplate.exchange( "https://rsbuddy.com/exchange/summary.json", httpmethod.get, null, new parameterizedtypereference<itemsummarycontainer>() {}); itemsummarycontainer container = response.getbody();
and jackson's objectmapper
, have:
objectmapper mapper = new objectmapper(); itemsummarycontainer container = mapper.readvalue(new url("https://rsbuddy.com/exchange/summary.json"), itemsummarycontainer.class);
Comments
Post a Comment