How to convert json array of K/V to Java HashMap with Jackson JSON -
i learning first java json parser librairie witch jackson json.
i'm trying convert java object list of id/note hashmap list.
my json input this
var basketlist = [ { "name": "basket 1", "productlist": { //id item incremente ordering "14":{ // quantity add id "quantity":6, "note": "thing" }, "15":{ "quantity":4, "note": "another thing" }, } }, { "name": "basket 2", "productlist": { "14":{ "quantity": 16, "note": "thing" }, "15":{ "quantity":2, "note": "another thing" }, "17":{ "quantity":7, "note": "some thing" } } }
]
my product list dynamic , don't want create java object that,
my first idea build new productlist in java , add each quantity right product id.
i can't find example online on how that, i'm trying use objectmapper().readtree() , play jsonnode
i can't make work, appreciated
i have done i'm stuck on how key name of last jsonnode :
string json = myjavaitem.getjson(); jsonnode javaitem = mapper.readtree( json ); list<product> listiwantcreate = buildatestorderlist( javaitem ); public static list<product> buildatestorderlist( jsonnode node ) { list<product> productlist = new arraylist<product>(); jsonnode cabinetlist = node.path( "cabinet" ); if ( !cabinetlist.ismissingnode() ) { ( jsonnode cabinet : cabinetlist ) { jsonnode basketlist= cabinet.path( "basketlist" ); if ( !basketlist.ismissingnode() ) { ( jsonnode item : productlist ) { // need populate here integer iditem; // how key of current item ? integer qtity = item.path( "quantity" ).getintvalue(); product p = new product(); p.setiditem( iditem ); p.setquantity(qtity); productlist.add( p ); } } } } return productlist ; }
what looking method fields()
of jsonnode
:
for (iterator<entry<string, jsonnode>> iterator = basketlist.fields(); iterator.hasnext();) { entry<string, jsonnode> item = iterator.next(); integer iditem = integer.parseint(item.getkey()); // snip }
Comments
Post a Comment