java解析json字符串

来源:互联网 发布:淘宝部分店铺无法使用 编辑:程序博客网 时间:2024/04/30 18:02
 public void getJSONObject(){
        JSONObject node = JSONObject.fromObject("{'weatherinfo':
{'city':'北京','cityid':'101010100','temp1':'-8℃','temp2':'4℃',
'weather':'晴','img1':'n0'}}");
        List<Object>  nodes = new ArrayList<Object>();
    
        JSONArray jsons = JSONArray.fromObject(node.get("weatherinfo"));
         
        for (Object o : jsons)
        {
            JSONObject jsonNode = JSONObject.fromObject(o);
            List<Object> treeNodes = new ArrayList<Object>();
            treeNodes.add(jsonNode.getString("city"));
            treeNodes.add(jsonNode.getString("cityid"));
            treeNodes.add(jsonNode.getString("temp1"));
            treeNodes.add(jsonNode.getString("temp2"));
            //...
            nodes.add(treeNodes);
        }
     
    System.out.println(nodes);
    }
0 0