如何订购JSONArray?

时间:2012-03-08 03:54:43

标签: java json jsp

我写了一些jsp来生成json,但在一种情况下,我遇到了一个问题, 我下订单并获得订单不一样, 我把1,2,3,4,5放到jsonarray,但似乎给我一个错误的订单3,2,5,1,4 这是我在jsp中的代码:

Map map = new LinkedHashMap();  
Iterator itor = listView.getList().iterator();
JSONObject json = new JSONObject();
JSONArray jArray = new JSONArray();

while ( itor.hasNext() ) {
    current = (EvidPackageDao) itor.next();
    map.put("id", current.getPackageId());
    map.put("packno", current.getPackageNo());
    map.put("photocover", current.getPhotoCover());
    map.put("photoback", current.getPhotoBack());
    map.put("evidi", current.getEvidIndentification());
    map.put("type", current.getEvidType());
    map.put("verno", current.getVersionNo());
    map.put("isbn", current.getIsbn());
    map.put("pub", current.getPublisher());
    map.put("distr", current.getDistributor());
    map.put("producer", current.getProducer());
    map.put("cprl", current.getCoptrightLogo());
    map.put("cprt", current.getCopyrightText());
    map.put("reason", current.getIllegalReason());

    Iterator it = map.entrySet().iterator();

    while (it.hasNext()) 
            {
           Map.Entry entry = (Map.Entry) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        System.out.println("key=" + key + ", value=" + value);

    }

            /*
             print:
        key=id, value=81
        key=packno, value=fsdfdsfsdf
        key=photocover, value=null
        key=photoback, value=null
        key=evidi, value=sdfsadf
        key=type, value=dsfsdf
        key=verno, value=sdfasdfl
        key=isbn, value=3224jsfklf
        key=pub, value=fdsjkla
        key=distr, value=lkfjl;dsfklj
        key=producer, value=fdafjdsl;
        key=cprl, value=sdfjadlfkj
        key=cprt, value=sdfadff
        key=reason, value=fdsfdsafasf

            */
    jArray.put(map);
    System.out.println(jArray.toString());
            /*
            print:
               [{"reason":null,"distr":null,"type":null,"pub":null,"verno":null,"id":82,"cprl":null,"isbn":null,"evidi":null,"photoback":null,"producer":null,"packno":null,"photocover":null,"cprt":null},{"reason":"fdsfdsafasf","distr":"lkfjl;dsfklj","type":"dsfsdf","pub":"fdsjkla","verno":"sdfasdfl","id":81,"cprl":"sdfjadlfkj","isbn":"3224jsfklf","evidi":"sdfsadf","photoback":null,"producer":"fdafjdsl;","packno":"fsdfdsfsdf","photocover":null,"cprt":"sdfadff"}]
            */

}

那么我怎样才能得到正确的顺序与我的put到hashmap ???????比如[{“id”:null,?

1 个答案:

答案 0 :(得分:2)

您使用的是哪个库?我正在使用json-simple库,以下代码可以工作:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Map map = new LinkedHashMap();
    JSONArray jArray = new JSONArray();


    map.put("id", "aaaaaa");
    map.put("packno", "bbbbb");
    map.put("photocover", "cccccc");
    map.put("photoback", "ddddd");
    map.put("evidi", "eeeeee");
    map.put("type", "ffffff");


    jArray.add(map);
    System.out.println(jArray.toString());

}

输出:

[{"id":"aaaaaa","packno":"bbbbb","photocover":"cccccc","photoback":"ddddd","evidi":"eeeeee","type":"ffffff"}]