java解析json文本
2011-07-29 15:20
495 查看
今天写自动化脚本时候,发现有一个页面的数据是通过json文本样式显示的,在浏览器上看json文本真的不好看。看得眼花缭乱的。后来通过与开发人员了解,json文本是可以通过各种语言去解析的。所有就花了1个小时的时间看了一下json的解析方法。
1、需要各种java的架包:commons-beanutils.jar commons-collections-3.2.1.jar commons-httpclient-3.0.jar commons-lang-2.4.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar morph-1.1.1.jar
为了实现这个功能需要引入这么多架包,哎!
2、{"list":[{"DocId":"75","Title":"China Banking Watch (Chinese edition) ","DisplayTime":"2011-05-02","DisplayTimeUTCUnix":1304395200,"FileType":"pdf","Synopsis":" ","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=75","InternalFilename":"75_1_16.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Emerging Markets"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"Xia Le"}],"IsInBriefcase":0},{"DocId":"81","Title":"German consumers: Will they or won’t they?","DisplayTime":"2011-05-01","DisplayTimeUTCUnix":1304308800,"FileType":"pdf","Synopsis":"The impressive rebound of the German economy in 2010 and in particular the ongoing improvement in the labour market has fuelled expectations that consumption will become a major engine of German growth in 2011/2012*. This perception has been corroborated by a clear improvement in German consumer confidence and almost euphoric responses of German retailers in the monthly ifo business climate survey. Both the assessment of the current situation and the expectations for the next 6 months are close to their all-time-highs recorded at the beginning of 1991, when the German unification boom was in full swing. In addition, the German retail association commented on the beginning of the Christmas sales with expressions such as “dream start”.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=81","InternalFilename":"81_1_10.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"S. Schneider"}],"IsInBriefcase":0},{"DocId":"83","Title":"Fine-Tuning Your Portfolio for the Year Ahead","DisplayTime":"2011-01-13","DisplayTimeUTCUnix":1294981200,"FileType":"pdf","Synopsis":"In the report, we re-examine the case for our current asset allocation and highlight some tactical changes to our asset allocation for US clients. We maintain our current tactical allocation of 65% equities, 30% bonds, and 5% cash for a moderate investor. We believe equities will once again outperform bonds in 2011, driven by accommodative policy, rotation out of fixed income markets, and an improving macro economic backdrop.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=83","InternalFilename":"83_1_3.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"Michael Hartnett"}],"IsInBriefcase":0},{"DocId":"85","Title":"Morgan Stanley Global Communicator — June 04","DisplayTime":"2004-06-28","DisplayTimeUTCUnix":1088481600,"FileType":"pdf","Synopsis":"Four of our six leading indicators for the wireline equipment industry were positive in June, up from three in May. Our global carrier capex forecast was revised upward and carrier revenue estimates for 2004 and 2005 have improved. The most recent revision to global GDP estimates by our chief economist, Stephen Roach, was positive, and total connections were up in 1Q04.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=85","InternalFilename":"85_1_1.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Rating Change"},{"Name":"Emerging Markets"},{"Name":"Economics"}],"Issuers":[{"Name":"PWAV","PrimarySymbolId":"43747"}],"Analysts":[{"Name":"Alkesh Shah"}],"IsInBriefcase":0}],"count":4,"CategoryName":"Fixed Income"}
这个就是我需要解析的json文本,这样看真不好看
3、从这段json文本中可以看出json的格式是{key,value}形式,但我们发现这个里面有一个这样格式:"list":[{key,value},{key,value}],后来才知道这个是json的数组形式
4、解析来看我是怎么解析这段json文本的“Title”属性的
String json="上面那段json文本";
JSONObject jsonObj = JSONObject.fromObject(json);
JSONArray jsonArray = jsonObj.getJSONArray("list");
HashMap<String,String> titleMap=new HashMap<String,String>();
for(int i=0;i<jsonArray.size();i++){
String eachList=jsonArray.getString(i);
jsonObj = JSONObject.fromObject(eachList);
String title=jsonObj.getString("Title").trim();
titleMap.put("title"+i, title);
}
好了,把需要的一些架包放上吧,以便帮助到别人。
1、需要各种java的架包:commons-beanutils.jar commons-collections-3.2.1.jar commons-httpclient-3.0.jar commons-lang-2.4.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar morph-1.1.1.jar
为了实现这个功能需要引入这么多架包,哎!
2、{"list":[{"DocId":"75","Title":"China Banking Watch (Chinese edition) ","DisplayTime":"2011-05-02","DisplayTimeUTCUnix":1304395200,"FileType":"pdf","Synopsis":" ","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=75","InternalFilename":"75_1_16.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Emerging Markets"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"Xia Le"}],"IsInBriefcase":0},{"DocId":"81","Title":"German consumers: Will they or won’t they?","DisplayTime":"2011-05-01","DisplayTimeUTCUnix":1304308800,"FileType":"pdf","Synopsis":"The impressive rebound of the German economy in 2010 and in particular the ongoing improvement in the labour market has fuelled expectations that consumption will become a major engine of German growth in 2011/2012*. This perception has been corroborated by a clear improvement in German consumer confidence and almost euphoric responses of German retailers in the monthly ifo business climate survey. Both the assessment of the current situation and the expectations for the next 6 months are close to their all-time-highs recorded at the beginning of 1991, when the German unification boom was in full swing. In addition, the German retail association commented on the beginning of the Christmas sales with expressions such as “dream start”.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=81","InternalFilename":"81_1_10.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"S. Schneider"}],"IsInBriefcase":0},{"DocId":"83","Title":"Fine-Tuning Your Portfolio for the Year Ahead","DisplayTime":"2011-01-13","DisplayTimeUTCUnix":1294981200,"FileType":"pdf","Synopsis":"In the report, we re-examine the case for our current asset allocation and highlight some tactical changes to our asset allocation for US clients. We maintain our current tactical allocation of 65% equities, 30% bonds, and 5% cash for a moderate investor. We believe equities will once again outperform bonds in 2011, driven by accommodative policy, rotation out of fixed income markets, and an improving macro economic backdrop.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=83","InternalFilename":"83_1_3.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"Economics"}],"Issuers":[],"Analysts":[{"Name":"Michael Hartnett"}],"IsInBriefcase":0},{"DocId":"85","Title":"Morgan Stanley Global Communicator — June 04","DisplayTime":"2004-06-28","DisplayTimeUTCUnix":1088481600,"FileType":"pdf","Synopsis":"Four of our six leading indicators for the wireline equipment industry were positive in June, up from three in May. Our global carrier capex forecast was revised upward and carrier revenue estimates for 2004 and 2005 have improved. The most recent revision to global GDP estimates by our chief economist, Stephen Roach, was positive, and total connections were up in 1Q04.","ReferenceId":"","ResourceURL":"/Intellicast/api/get_doc.json?userid=5084&clienttypeid=0&themeid=135&docid=85","InternalFilename":"85_1_1.pdf","Categories":[{"Name":"Equity"},{"Name":"Fixed Income"},{"Name":"FX/FI Strategy"},{"Name":"Rating Change"},{"Name":"Emerging Markets"},{"Name":"Economics"}],"Issuers":[{"Name":"PWAV","PrimarySymbolId":"43747"}],"Analysts":[{"Name":"Alkesh Shah"}],"IsInBriefcase":0}],"count":4,"CategoryName":"Fixed Income"}
这个就是我需要解析的json文本,这样看真不好看
3、从这段json文本中可以看出json的格式是{key,value}形式,但我们发现这个里面有一个这样格式:"list":[{key,value},{key,value}],后来才知道这个是json的数组形式
4、解析来看我是怎么解析这段json文本的“Title”属性的
String json="上面那段json文本";
JSONObject jsonObj = JSONObject.fromObject(json);
JSONArray jsonArray = jsonObj.getJSONArray("list");
HashMap<String,String> titleMap=new HashMap<String,String>();
for(int i=0;i<jsonArray.size();i++){
String eachList=jsonArray.getString(i);
jsonObj = JSONObject.fromObject(eachList);
String title=jsonObj.getString("Title").trim();
titleMap.put("title"+i, title);
}
好了,把需要的一些架包放上吧,以便帮助到别人。
相关文章推荐
- 解析Json文本——如何将Json文本转化为Java对象
- 解析Json文本——如何将Json文本转化为Java对象
- java解析json文本,去除//后和/**/中的内容
- java解析json
- java解析json字符串
- Java调用天气接口(百度天气)解析返回的JSON数据
- java解析json
- java解析json
- java 解析JSON字符串
- Java构造和解析Json数据的两种方法详解2
- Java解析json文件的方法--直接拷贝可以使用
- java解析任意形式的json,树型结构展示
- java 生成json与解析
- c++ & java解析JSON格式数组
- java解析和读取JSON
- Android开发 Java解析Json字符串
- 一个解析Json文件的java实现
- Java下利用Jackson进行JSON解析和序列化
- JAVA解析JSON
- Java基础系列16:使用JSONObject和JSONArray解析和构造json字符串