简体   繁体   中英

How to convert json array of K/V to Java HashMap with Jackson JSON

I am learning my first Java Json parser librairie witch is Jackson JSON.

I'm trying to convert as a Java Object a List of ID/NOTE into a HashMap list.

My Json input look like this

var basketList = [
{
    "name": "Basket 1",
    "productList": {
        //Id Item to incremente for ordering
        "14":{
            // quantity to be add to this 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 is dynamic and I don't want to create a Java Object for that,

My first Idea was to build a new productList in java and add each quantity to the right product id.

I can't find any example online on how to do that, I'm trying to use ObjectMapper().readTree() and play with JsonNode

I can't make it work, any help would be appreciated

I have done this but I'm stuck on how to get the Key name of my 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() )
    {
        for ( JsonNode cabinet : cabinetList )
        {
            JsonNode basketList= cabinet.path( "basketList" );
            if ( !basketList.isMissingNode() )
            {
                for ( JsonNode item : productList )
                {
                   // I need to populate here
                   Integer idItem; // how to get the 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 you are looking for is the 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
}

you can use TypeFactory of jackson mapper with a code something like this..

objectMapper.readValue(yourJsonString, TypeFactory.mapType(Map.class, String.class, TypeFactory.collectionType(List.class, Product.class));

assuming your Product looks like this.

class Product {

   int quantity;
   String note;
   //getter - setter

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM