首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

用java解析json字符串有关问题

2012-03-16 
用java解析json字符串问题Java code{weatherinfo:{city:北京,cityid:101010100,temp1:-8℃,

用java解析json字符串问题

Java code
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"-8℃","temp2":"4℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}

请问这个怎么解析呢?

[解决办法]
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


Java code
 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);    } 

热点排行