自定义Json格式

老铁们都知道,一般的json格式就是键值对格式,在一些特定的框架或者系统中,会用到自定义格式的json文件,假设我们要得到的特定格式json格式如下:

{"A":"2","B":20,"C":"38","D":[{"E":"232","cell":["a","b","c","d"]},{"E":"235","cell":["a1","b1","c1","d1"]}]}

那么代码就可以如下写法:

public static JSONObject List2Json(List list) {
               
        JSONObject json = new JSONObject();
        json.put("A", "2");
        prologjson.put("B", 20);
        prologjson.put("C", "38");
        
        for(int i = 0; i < 某条件; i++){
            JSONObject tempobj = new JSONObject();
            tempobj.put("E", list.get(i).getA());
            tempobj.accumulate("cell", list.get(i).getA);
            tempobj.accumulate("cell", list.get(i).getB);
            tempobj.accumulate("cell", list.get(i).getC);
            tempobj.accumulate("cell", list.get(i).getD));
            prologjson.accumulate("rows", tempobj);
        }
        return prologjson;
    }

 ps:以上的代码是伪代码,复制粘贴是运行不了的哦。

原文地址:https://www.cnblogs.com/K-artorias/p/6877797.html