用java解析json字符串问题

daishunchaoaiqiu 2012-02-08 10:49:40
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"-8℃","temp2":"4℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}

请问这个怎么解析呢?
...全文
10330 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
z1313586 2014-09-06
  • 打赏
  • 举报
回复
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 是怎么回事呢
zuxianghuang 2014-08-13
  • 打赏
  • 举报
回复
http://json-lib.sourceforge.net/ 我用的json-lib-jdk1.5
引用 11 楼 zidiancao 的回复:
哪个包啊? [quote=引用 8 楼 ZuxiangHuang 的回复:] 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);
    }

[/quote]
zidiancao 2014-08-13
  • 打赏
  • 举报
回复
哪个包啊?
引用 8 楼 ZuxiangHuang 的回复:
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);
    }

zhangxiaoyan06 2012-02-09
  • 打赏
  • 举报
回复
也可以使用json-simple的各种解析
示例和jar下载地址如下:
http://code.google.com/p/json-simple/downloads/list
demohunter 2012-02-09
  • 打赏
  • 举报
回复
如果你想要得到里面的信息的话 可以先在前台用json解析把它封装到Weatherinfo这个对象里面 再把Weatherinfo传到后台不就得了嘛 干嘛非要用java解析json呢
zuxianghuang 2012-02-09
  • 打赏
  • 举报
回复
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);
}

c1star 2012-02-09
  • 打赏
  • 举报
回复
JSONObject jsonObj = (JSONObject)jParser.parse("楼主的字符串");
Object obj = jsonObj .get ("weatherinfo");
JSONObject subObj = (JSONObject)obj ;
String city= (String)subObj .get("city");
  • 打赏
  • 举报
回复
用jackson-mapper-lgpl-1.9.2.jar 里面有各种解析
wocaniooxxoo 2012-02-09
  • 打赏
  • 举报
回复
public JSONObject getJSONObject(String key) 或者用这个方法也可以试试
wocaniooxxoo 2012-02-09
  • 打赏
  • 举报
回复
可以先创建个jsonobjct jsonob=new jsonobject(json) 然后hashmap map=(hashmap)jsonob.get(“weatherinfo”)试试 看看能不能弄到hashmap里面
  • 打赏
  • 举报
回复
有org.json
学习下呗
daishunchaoaiqiu 2012-02-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lightning_16 的回复:]

用jquery,返回的data.weatherinfo.city
[/Quote]
我想后台java解析出来
lightning_16 2012-02-08
  • 打赏
  • 举报
回复
用jquery,返回的data.weatherinfo.city

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧