Wish E:\projects\test

icefairy333

Active Member
Licensed User
Longtime User
wish keep the order of jsonobject(init with an linkedhashmap)
B4X:
public static void main(String[] args) {
        // TODO Auto-generated method stub
        String url="http://trend.caipiao.163.com/cqssc/jiben-5xing.html?periodNumber=30";
        String useragent="Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1";
        try {
            Document doc= Jsoup.connect(url).userAgent(useragent).timeout(5000).get();
            Element table=doc.getElementById("cpdata");
            Elements lines=table.children();
            LinkedHashMap<String, Object> map=new LinkedHashMap<>();
            for(int i=0;i<lines.size();i++){
                Element line=lines.get(i);
                Elements tds=line.children();
                String qh=tds.get(0).text().trim();
                String hm=tds.get(2).text().trim();
                if(qh.length()>0&&hm.length()>0){
                map.put(qh, hm);
                }
            }
            System.out.println(org.json.simple.JSONObject.toJSONString(map));//this will keep the order of LinkedHashMap
            JSONObject jo=new JSONObject(map);   
            System.out.println(jo.toString(1));//this will not keep the order of LinkedHashMap
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
    }
 

Attachments

  • jsonordertest.zip
    392.3 KB · Views: 202
  • jsoup-1.10.2.jar
    342.4 KB · Views: 201
Top