用java解析json字符串问题 用jquery,返回的data.weatherinfo.city 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以先创建个jsonobjct jsonob=new jsonobject(json) 然后hashmap map=(hashmap)jsonob.get(“weatherinfo”)试试 看看能不能弄到hashmap里面 public JSONObject getJSONObject(String key) 或者用这个方法也可以试试 用jackson-mapper-lgpl-1.9.2.jar 里面有各种解析 JSONObject jsonObj = (JSONObject)jParser.parse("楼主的字符串");Object obj = jsonObj .get ("weatherinfo");JSONObject subObj = (JSONObject)obj ;String city= (String)subObj .get("city"); import net.sf.json.JSONArray;import net.sf.json.JSONObject; 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); } 如果你想要得到里面的信息的话 可以先在前台用json解析把它封装到Weatherinfo这个对象里面 再把Weatherinfo传到后台不就得了嘛 干嘛非要用java解析json呢 也可以使用json-simple的各种解析示例和jar下载地址如下:http://code.google.com/p/json-simple/downloads/list http://json-lib.sourceforge.net/我用的json-lib-jdk1.5 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 是怎么回事呢 菜鸟问题之三:根据提交选择包含文件 网站开发方面的问题 这是一个菜单JS代码,怎么插入到页面中?? 用JSP如何实现抓屏 急求各位大侠 common-net上传文件中文问题 多用户JSP电子商城出售,只需要500 USE 求一个fckeditor运行在jsp中的案例,在线等 斑竹~得分来干什么??? 一个奇怪的路径问题,求高手! 求分页的一些技术
Object obj = jsonObj .get ("weatherinfo");
JSONObject subObj = (JSONObject)obj ;
String city= (String)subObj .get("city");
import net.sf.json.JSONObject; 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);
}
示例和jar下载地址如下:
http://code.google.com/p/json-simple/downloads/list
我用的json-lib-jdk1.5