如何在Java中将列表数据转换为JSON

如何在Java中将列表数据转换为JSON,第1张

如何在Java中将列表数据转换为JSON
public static List<Product> getCartList() {    JSonObject responseDetailsJson = new JSonObject();    JSonArray jsonArray = new JSonArray();    List<Product> cartList = new Vector<Product>(cartMap.keySet().size());    for(Product p : cartMap.keySet()) {        cartList.add(p);        JSonObject formDetailsJson = new JSonObject();        formDetailsJson.put("id", "1");        formDetailsJson.put("name", "name1");       jsonArray.add(formDetailsJson);    }    responseDetailsJson.put("forms", jsonArray);//Here you can see the data in json format    return cartList;}

您可以通过以下形式获取数据

{    "forms": [        { "id": "1", "name": "name1" },        { "id": "2", "name": "name2" }     ]}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5622856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存