java解析json数据

1.背景

最近在解析json的数据,发现直接用HQL逻辑很繁琐,于是借助java,再添加为UDF。

2.什么是json(参考:http://www.json.org/)

json一共有两种数据结构

(1)以(key/value)对形式存在的无序的jsonObject对象,其以"{"(左括号)开始,以"}"(右括号)结束。

格式如下:

{
"名称1":"值1",
"名称2":"值2",
"名称3":"值3"	
}



名称和值之间用":"(冒号)对应,不同名称/值之间使用","(逗号)分隔。下面举一个简单的例子。

{
"name":"xiaoming",
"age":10,
"sex":"male"	
}



这就是一个最简单的json对象,对于这种数据格式,key必须是string,value可以是string,number,object,array,true,false,null等等数据类型。

(2)有序的value集合,这种形式被称为jsonArray,数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

{
	"employee":[
	{"name":"John","age":20,"sex":"男"},
	{"name":"Anna","age":25,"sex":"男"},
	{"name":"Peter","age":22,"sex":"男"}
	]
}



在上面的例子中,对象employee是包含三个对象的数组。每个对象包含某个员工的信息(姓名,年龄,性别)。

3.举例

在这里我们可以学到的是:

(1)区别JSONObject与JSONArray

(2)能够解析json数据

下面是一个json文件:

{
    "resultcode": "200",
    "reason": "successed!",
    "result": {
        "sk": {
            "temp": "24",
            "wind_direction": "西南风",
            "wind_strength": "2级",
            "humidity": "51%",
            "time": "10:11"
        },
        "today": {
            "temperature": "16℃~27℃",
            "weather": "阴转多云",
            "weather_id": {
                "fa": "02",
                "fb": "01"
            },
            "wind": "西南风3-4 级",
            "week": "星期四",
            "city": "滨州",
            "date_y": "2015年06月04日",
            "dressing_index": "舒适",
            "dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
            "uv_index": "最弱",
            "comfort_index": "",
            "wash_index": "较适宜",
            "travel_index": "",
            "exercise_index": "较适宜",
            "drying_index": ""
        },
        "future": [
            {
                "temperature": "16℃~27℃",
                "weather": "阴转多云",
                "weather_id": {
                    "fa": "02",
                    "fb": "01"
                },
                "wind": "西南风3-4 级",
                "week": "星期四",
                "date": "20150604"
            },
            {
                "temperature": "20℃~32℃",
                "weather": "多云转晴",
                "weather_id": {
                    "fa": "01",
                    "fb": "00"
                },
                "wind": "西风3-4 级",
                "week": "星期五",
                "date": "20150605"
            },
            {
                "temperature": "23℃~35℃",
                "weather": "多云转阴",
                "weather_id": {
                    "fa": "01",
                    "fb": "02"
                },
                "wind": "西南风3-4 级",
                "week": "星期六",
                "date": "20150606"
            },
            {
                "temperature": "20℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "北风微风",
                "week": "星期日",
                "date": "20150607"
            },
            {
                "temperature": "22℃~34℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "西南风3-4 级",
                "week": "星期一",
                "date": "20150608"
            },
            {
                "temperature": "22℃~33℃",
                "weather": "阴",
                "weather_id": {
                    "fa": "02",
                    "fb": "02"
                },
                "wind": "西南风3-4 级",
                "week": "星期二",
                "date": "20150609"
            },
            {
                "temperature": "22℃~33℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "南风3-4 级",
                "week": "星期三",
                "date": "20150610"
            }
        ]
    },
    "error_code": 0
}



我们进行解析:
package com.exercise;

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 Analyse_json {

	public static void main(String args[]) {

		String Path = "E:/WorkSpaces/test.json";
		JsonParser parser = new JsonParser();// 创建json解析器
		try {
			// 创建JsonObject对象
			JsonObject object = (JsonObject) parser.parse(new FileReader(Path));
			// 将json数据转为int型的数据
			System.out.println("resultcode:" + object.get("resultcode").getAsInt());
			// 将json数据转为String型的数据
			System.out.println("reason:" + object.get("reason").getAsString());

			JsonObject result = object.get("result").getAsJsonObject();
			JsonObject today = result.get("today").getAsJsonObject();
			System.out.println("\n今天的天气情况");
			System.out.println("temperature:" + today.get("temperature").getAsString());
			System.out.println("weather:" + today.get("weather").getAsString());

			// 得到为json的数组

			JsonArray array = result.get("future").getAsJsonArray();
			System.out.println("\n未来几天的天气情况");
			for (int i = 0; i < array.size(); i++) {

				System.out.println("-----------------------");
				JsonObject subObject = array.get(i).getAsJsonObject();
				System.out.println("temperature:" + subObject.get("temperature").getAsString());
				System.out.println("weather:" + subObject.get("weather").getAsString());
				System.out.println("wind:" + subObject.get("wind").getAsString());
				System.out.println("date:" + subObject.get("date").getAsString());

			}

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

	}

}


输出结果:

resultcode:200
reason:successed!


今天的天气情况
temperature:16℃~27℃
weather:阴转多云


未来几天的天气情况
-----------------------
temperature:16℃~27℃
weather:阴转多云
wind:西南风3-4 级
date:20150604
-----------------------
temperature:20℃~32℃
weather:多云转晴
wind:西风3-4 级
date:20150605
-----------------------
temperature:23℃~35℃
weather:多云转阴
wind:西南风3-4 级
date:20150606
-----------------------
temperature:20℃~33℃
weather:多云
wind:北风微风
date:20150607
-----------------------
temperature:22℃~34℃
weather:多云
wind:西南风3-4 级
date:20150608
-----------------------
temperature:22℃~33℃
weather:阴
wind:西南风3-4 级
date:20150609
-----------------------
temperature:22℃~33℃
weather:多云
wind:南风3-4 级
date:20150610

我们对上面进行解释:
1.首先创建JsonObject对象object,相对其来讲resultcode,reason,result,error_code是并列的。这是很明显的
JsonObject
2.对于result对象来说,sk,today,future是并列的。这是很明显的JsonObject
3.剖析future来讲,这是一个JSONArray,如果想要解析里面的信息,需要先get json数组。通过
JsonArray array = result.get("future").getAsJsonArray();实现
分析:
我们通过Gson进行解析,所以在使用前导入Gson.jar
(pom如下:
 <dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.7</version>
		</dependency>
)
解析json数据时,
1.需要进行创建Gson解析器
2.创建JsonObject对象
3.将json数据转为相应的数据

4.参考
博客
http://www.cnblogs.com/boy1025/p/4551593.html
http://www.cnblogs.com/xiaoluo501395377/p/3446605.html









































  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值