0

我使用以下代码打印 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 ); 

我把itemstotalPage放到了map,而在items中,还有其他map在一个ArrayList..

现在结果看起来像:它是根上的数组

[{
    "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"
            }
       ]}

我能怎么做??

4

2 回答 2

3

您可以使用 :

JSONObject jsonObject = new JSONObject();
jsonObject.put("totalPage",il.size());
jsonObject.put("items",itemsList);
于 2013-10-21T03:14:46.833 回答
0
JSONArray jsonArray = JSONArray.fromObject( fatherList );  

如果你不想要一个数组,就不要做一个。

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

于 2013-10-21T03:04:40.607 回答