java解析json的操作

import java.io.FileNotFoundException;
import java.io.FileReader;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class ReadJSON {
    public static void main(String args[]){
        try {

            JsonParser parser=new JsonParser();  //创建JSON解析器

            //JsonObject result=parser.get("result").getAsJsonObject();

            JsonObject object=(JsonObject) parser.parse(new FileReader("weather.json"));  //创建JsonObject对象
             //+++++++++++++++++++++++++++是json中的主变量+++++++++++++++++++++++++++
            System.out.println("resultcode"+object.get("resultcode").getAsInt());
            System.out.println("reason"+object.get("reason").getAsString());
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            //+++++++++++++++++++++到主json的目录result中   包括 迭代json object 和json 数组
            JsonObject result1=object.get("result").getAsJsonObject();

            //++++++++++++++++++进入到result中的子object json之中++++++++++++++++++++++++++++++++++++++++++++++++++
            JsonObject today=result1.get("today").getAsJsonObject();
            System.out.println("temperature:"+today.get("temperature").getAsString());
            System.out.println("weather:"+today.get("weather").getAsString());
            System.out.println("city"+today.get("city").getAsString());
            //today下面的迭代weather_id
            JsonObject weather_id=today.get("weather_id").getAsJsonObject();
            System.out.println("favalue:"+weather_id.get("fa").getAsString());
            //+++++++++++++++++通过result1 找到 存储数据的子目录   ARRAY    future,
            JsonArray array=result1.get("future").getAsJsonArray();    //得到为json的数组
            for(int i=0;i<array.size();i++){
                System.out.println("---------------");
                JsonObject subObject=array.get(i).getAsJsonObject();
                System.out.println("id="+subObject.get("temperature").getAsString());
                System.out.println("name="+subObject.get("weather").getAsString());
                System.out.println("week="+subObject.get("week").getAsString());
                System.out.println("wind"+subObject.get("wind").getAsString());
                //System.out.println("ide="+subObject.get("ide").getAsString());
            }

        } catch (JsonIOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

他所解析的json为

  1 {
  2   "resultcode": "200",
  3   "reason": "successed!",
  4   "result": {
  5     "sk": {
  6       "temp": "24",
  7       "wind_direction": "西南风",
  8       "wind_strength": "2级",
  9       "humidity": "51%",
 10       "time": "10:11"
 11     },
 12     "today": {
 13       "temperature": "16℃~27℃",
 14       "weather": "阴转多云",
 15       "weather_id": {
 16         "fa": "02",
 17         "fb": "01"
 18       },
 19       "wind": "西南风3-4 级",
 20       "week": "星期四",
 21       "city": "滨州",
 22       "date_y": "2015年06月04日",
 23       "dressing_index": "舒适",
 24       "dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
 25       "uv_index": "最弱",
 26       "comfort_index": "",
 27       "wash_index": "较适宜",
 28       "travel_index": "",
 29       "exercise_index": "较适宜",
 30       "drying_index": ""
 31     },
 32     "future": [
 33       {
 34         "temperature": "16℃~27℃",
 35         "weather": "阴转多云",
 36         "weather_id": {
 37           "fa": "02",
 38           "fb": "01"
 39         },
 40         "wind": "西南风3-4 级",
 41         "week": "星期四",
 42         "date": "20150604"
 43       },
 44       {
 45         "temperature": "20℃~32℃",
 46         "weather": "多云转晴",
 47         "weather_id": {
 48           "fa": "01",
 49           "fb": "00"
 50         },
 51         "wind": "西风3-4 级",
 52         "week": "星期五",
 53         "date": "20150605"
 54       },
 55       {
 56         "temperature": "23℃~35℃",
 57         "weather": "多云转阴",
 58         "weather_id": {
 59           "fa": "01",
 60           "fb": "02"
 61         },
 62         "wind": "西南风3-4 级",
 63         "week": "星期六",
 64         "date": "20150606"
 65       },
 66       {
 67         "temperature": "20℃~33℃",
 68         "weather": "多云",
 69         "weather_id": {
 70           "fa": "01",
 71           "fb": "01"
 72         },
 73         "wind": "北风微风",
 74         "week": "星期日",
 75         "date": "20150607"
 76       },
 77       {
 78         "temperature": "22℃~34℃",
 79         "weather": "多云",
 80         "weather_id": {
 81           "fa": "01",
 82           "fb": "01"
 83         },
 84         "wind": "西南风3-4 级",
 85         "week": "星期一",
 86         "date": "20150608"
 87       },
 88       {
 89         "temperature": "22℃~33℃",
 90         "weather": "阴",
 91         "weather_id": {
 92           "fa": "02",
 93           "fb": "02"
 94         },
 95         "wind": "西南风3-4 级",
 96         "week": "星期二",
 97         "date": "20150609"
 98       },
 99       {
100         "temperature": "22℃~33℃",
101         "weather": "多云",
102         "weather_id": {
103           "fa": "01",
104           "fb": "01"
105         },
106         "wind": "南风3-4 级",
107         "week": "星期三",
108         "date": "20150610"
109       }
110     ]
111   }
112 }

原文地址:https://www.cnblogs.com/tangsonghuai/p/10195146.html

时间: 2024-11-08 19:30:30

java解析json的操作的相关文章

Java解析JSON对象

今天学习一下Java 解析 JSON 准备工作 参考文档 1) www.json.org 2 ) 参考公司其他人的代码(这个就不说了) 依赖jar包 1) org.json.jar 提供以下我的网盘链接 http://pan.baidu.com/s/1jG2zSwe 主要的类 JSON主要有两种结构 1)name/value 的集合 2)value 的集合,在大多数语言中的list,array等,这里的 value 个人觉得就是一个JSON,也就是说是JSON的集合 JAVA处理JSON的类 1

java 解析JSON字符串

JAVA解析json字符串:根据城市itemName名称获取其码itemCode 1.json字符串 [{"childList":        [{"childList":            [{"childList":[],"id":374,"itemCode":"110101","itemName":"东城区","itemNam

js与java对json的操作

JSON呢,是现在大部分,并且主流的传递数据的方式. 今天讲一下javascript的java对json的操作 提到js,当下就有一个比较主流的插件,vue.js,这个插件程序员没用过也都听说过吧, 非常方便的一个js插件,像js对于json的操作比如说json的话直接可以点出来,像数据的话 data[0]需要给下标才能点出来, java呢处理json就比较复杂了 import org.json.JSON; 需要导一下包,对于已经toString的字符串 可以使用JSONObject obj =

Java解析json报文案列

Java解析json报文 json报文如下: { "code": 0, "data": { "city": { "cityId": 284609, "counname": "中国", "name": "东城区", "pname": "北京市" }, "liveIndex": { "

java解析json

在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别.下面首先介绍用json-lib构造和解析Json数据的方法示例. 用org.son构造和解析Json数据的方法详解请参见我下一篇博文:Java构造和解析Json数据的两种方法详解二 一.介绍 JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json数据

JAVA解析JSON数据

转自:http://www.cnblogs.com/boy1025/p/4551593.html 3.解析JSON数据(小编使用的GSON进行json数据的解析) 3-1 [JSONObject的解析] 下面是一个json文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

JAVA解析JSON相关

一json-lib.jar开发包使用依赖包 json-lib.jar开发包使用需要依赖包以下开发包: Json-lib requires (at least) the following dependencies in your classpath: * jakarta commons-lang 2.4 * jakarta commons-beanutils 1.7.0 * jakarta commons-collections 3.2 * jakarta commons-logging 1.1

java 解析json

例<解析评论> //post方式请求 String url=“http://product.dangdang.com/comment/comment.php?product_id=60569472&datatype=1&page=1&filtertype=1&sysfilter=1&sorttype=1”: //请求后的json格式 HttpPost httpPost=new HttpPost(url); HttpClient defaultHttpCl

Java解析JSON文件的方法(一)

一.首先需要在Eclipse工程中导入相关的jar包,jar包参见链接:http://yunpan.alibaba-inc.com/share/link/NdA5b6IFK 二.提供一份待解析的json文件,apkinfo.json如下: [ { "name":"帐号", "package_name":"com.android.account", "check_version":"1.2"