Json数据解析

以下全部是用Java语言实现。


一、使用Gson实现json数据解析

        需要用到gson和guava的jar包。

        gson包:http://mvnrepository.com/artifact/com.google.code.gson/gson

        guava包:http://mvnrepository.com/artifact/com.google.guava/guava


二、完整的Java项目,将其导入到Eclipse即可

       https://github.com/guozhenqiang/JsonParser


三、Json测试文件json.txt

{
    "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
}

四、解析json的类ReadJson.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ReadJson {
	//定义json解析器
    private static JsonParser jsonParser=new JsonParser();
    //json测试文件
    private static File file=new File("json.txt");
    
    private static final Gson gson = new GsonBuilder().create();
    
	public static void main(String args[]) throws FileNotFoundException{
		
		FileReader input = new FileReader(file);
		
		JsonElement jsonElement=jsonParser.parse(input);
		JsonObject json=jsonElement.getAsJsonObject();
		
		//简单json的获取
		String reason=json.get("reason").getAsString();
		int resultcode=json.get("resultcode").getAsInt();
		System.out.println("reason:"+reason);
		System.out.println("resultcode:"+resultcode);
		
		//嵌套的json的获取
		JsonObject result=json.get("result").getAsJsonObject();
        JsonObject today=result.get("today").getAsJsonObject();
        String temperature=today.get("temperature").getAsString();
        System.out.println("temperature:"+temperature);
        
        //json数组的解析
        JsonArray array=result.get("future").getAsJsonArray();
        //方法一
//        for(int i=0;i<array.size();i++){
//        	JsonObject subject=array.get(i).getAsJsonObject();
//        	String subtemperature=subject.get("temperature").getAsString();
//        	System.out.println("subtemperature:"+subtemperature);
//        }
        //方法二
//        for(JsonElement element: array){
//        	JsonObject subject=element.getAsJsonObject();
//        	String subtemperature=subject.get("temperature").getAsString();
//        	System.out.println("subtemperature:"+subtemperature);
//        }
        //方法三
        List<JsonObject> subtemperatureList = gson.fromJson(result.get("future").getAsJsonArray(), new TypeToken<List<JsonObject>>() {
		}.getType());
        for(JsonObject subtemperature: subtemperatureList){
        	System.out.println("subtemperature:"+subtemperature.get("temperature").getAsString());
        }
        
		
	}
	
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值