如何使用json-lib制作json格式?

时间:2013-10-21 03:00:57

标签: java json json-lib

我打印json字符串使用此代码:

fatherList.put("totalPage",il.size());
il=item.loadList(si,ps," a.cat_id ="+itemCat," a.id ");

List itemsList=new ArrayList();
for(int i=0;i<il.size();i++)
{
    item=il.get(i);
     Map childMap = new HashMap();  
    childMap.put("id",item.getId());
    childMap.put("name",item.getName());

    childMap.put("part_number",item.getPartNumber());
    childMap.put("check",false);
    itemsList.add(childMap);


}

    fatherList.put("items",itemsList);
    JSONArray jsonArray = JSONArray.fromObject( fatherList );  
    out.print( jsonArray ); 

我将 totalPage 添加到map,而中还有其他mapArrayList ..

现在结果如下:它是根目录上的数组

[{
    "items" : [{
            "id" : 1669,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101669"
        }, {
            "id" : 1670,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101670"
        }, {
            "id" : 1671,
            "check" : false,
            "name" : "2118mm趟门衣柜",
            "part_number" : "DP101671"
        }, {
            "id" : 1672,
            "check" : false,
            "name" : "2118mm趟门衣柜",
            "part_number" : "DP101672"
        }, {
            "id" : 1673,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101673"
        }, {
            "id" : 1674,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101674"
        }, {
            "id" : 1675,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101675"
        }, {
            "id" : 1676,
            "check" : false,
            "name" : "1800mm趟门衣柜",
            "part_number" : "DP101676"
        }, {
            "id" : 1677,
            "check" : false,
            "name" : "2118mm趟门衣柜",
            "part_number" : "DP101677"
        }, {
            "id" : 1678,
            "check" : false,
            "name" : "2118mm趟门衣柜",
            "part_number" : "DP101678"
        }, {
            "id" : 1679,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101679"
        }, {
            "id" : 1680,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101680"
        }, {
            "id" : 1681,
            "check" : false,
            "name" : "2168mm趟门衣柜",
            "part_number" : "DP101681"
        }, {
            "id" : 1682,
            "check" : false,
            "name" : "2168mm趟门衣柜",
            "part_number" : "DP101682"
        }, {
            "id" : 1683,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101683"
        }, {
            "id" : 1684,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101684"
        }, {
            "id" : 1685,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101685"
        }, {
            "id" : 1686,
            "check" : false,
            "name" : "1850mm趟门衣柜",
            "part_number" : "DP101686"
        }, {
            "id" : 1687,
            "check" : false,
            "name" : "2168mm趟门衣柜",
            "part_number" : "DP101687"
        }, {
            "id" : 1688,
            "check" : false,
            "name" : "2168mm趟门衣柜",
            "part_number" : "DP101688"
        }
    ],
    "totalPage" : 134
}]

但我希望结果格式是这样的:

    { "totalPage": 134, "items": [{
                "id" : 1669,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101669"
            }, {
                "id" : 1670,
                "check" : false,
                "name" : "1800mm趟门衣柜",
                "part_number" : "DP101670"
            }
       ]}

我该怎么办?

2 个答案:

答案 0 :(得分:3)

您可以使用:

JSONObject jsonObject = new JSONObject();
jsonObject.put("totalPage",il.size());
jsonObject.put("items",itemsList);

答案 1 :(得分:0)

JSONArray jsonArray = JSONArray.fromObject( fatherList );  

如果您不想要阵列,请不要制作阵列。

JSON对象中的属性顺序无关紧要。