list转json字符串

时间:2025-04-02 14:31:01

使用Gson把list转成json字符串

    @GetMapping("/valueTest")
    public String valueTest() {
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("value",3339);
        map1.put("name","联盟广告");
        Map<String,Object> map2 = new HashMap<>();
        map2.put("value",1000);
        map2.put("name","test");
        Map<String,Object> map3 = new HashMap<>();
        map3.put("value",666);
        map3.put("name","邮件营销");
        list.add(map1);
        list.add(map2);
        list.add(map3);

        Gson gson =new Gson();
        String str = gson.toJson(list);
        return str;
    }

import ;

    String json = JSONArray.fromObject(list).toString();