Java Arraylist Data extraction

前端 未结 3 722
感情败类
感情败类 2021-01-25 06:47

How would you extract the data as follows:

I want to extract from this arraylist:

[{itemname=Original, number=12}, {itemname=BBQ, number=23}, {itemname=C         


        
3条回答
  •  旧巷少年郎
    2021-01-25 07:45

    To extract the data as you expected, you can use Jackson JSON processor. It allows you to read and write JSON easily. You can follow their tutorial here.

    Fist you have to download the relevant jar files(2 files) provided by them.

    So the following code snippet should solve your problem, and the result is written to the jsonResult.json file.

        String[] chips = {"Original", "BBQ", "CatchUp"};
        String[] chipentry = {"12", "23", "23"};
    
        List> list = new ArrayList>();
        for (int i = 0; i < 3; i++) {
            HashMap map = new HashMap();
            map.put("itemname", chips[i]);
            map.put("number", chipentry[i]);
            list.add(map);
        }
    
        ObjectMapper mapper = new ObjectMapper();
        Map untyped = new HashMap();
    
        untyped.put("result", list);
        mapper.writeValue(new File("jsonResult.json"), untyped);
    

    And following is the output of the file,

    {"result":[{"itemname":"Original","number":"12"},{"itemname":"BBQ","number":"23"},{"itemname":"CatchUp","number":"23"}]}
    

提交回复
热议问题