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
}

解析JSONObject

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException; import java.io.FileNotFoundException;
import java.io.FileReader; public class ReadJson {
public static void main(String []args) {
JsonParser parse = new JsonParser();
try {
JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json"));
System.out.println("resultcode:" + json.get("resultcodeu").getAsInt());
System.out.println("reason:" + json.get("reason").getAsString());
JsonObject result = json.get("result").getAsJsonObject();
JsonObject today = result.get("today").getAsJsonObject();
System.out.println("weak:" + today.get("week").getAsString());
System.out.println("weather:" + today.get("weather").getAsString());
} catch (JsonIOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (JsonSyntaxException e){
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

解析JSONArray

import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException; import java.io.FileNotFoundException;
import java.io.FileReader; public class ReadJsonArray {
public static void main(String []args) {
JsonParser parse = new JsonParser();
try {
JsonObject json = (JsonObject)parse.parse(new FileReader("C:\\Users\\jihite\\IdeaProjects\\TestProject\\jsontest\\src\\main\\java\\weather.json"));
JsonObject result = json.get("result").getAsJsonObject();
JsonArray futureArray = result.get("future").getAsJsonArray();
for (int i = 0; i < futureArray.size(); ++i) {
JsonObject subObj = futureArray.get(i).getAsJsonObject();
System.out.println("------");
System.out.println("week:" + subObj.get("week").getAsString());
System.out.println("weather:" + subObj.get("weather").getAsString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}
}

注意

文件路径相对路径是从工程根目录开始

Java 使用gson 解析 Json的更多相关文章

  1. JAVA使用Gson解析json数据,实例

    封装类Attribute: public class Attribute { private int id; private String name; private int age; public ...

  2. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  3. 使用Gson解析json

    前边的博客说过将json解析成java的方法,使用的是 这几个jar包,但是在解析时层遇到一个问题,就是在将时间字符串转换为java的Timestamp对象时会抛出异常,这个问题一直放在哪里没有去解决 ...

  4. 大话JSON之Gson解析JSON

    (三)解析Json数组(多条Json数据) 比如有如下Json数据: [{'name':'John', 'grade':[{'course':'English','score':100},{'cour ...

  5. Android中使用Gson解析JSON数据的两种方法

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下   Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...

  6. Android网络之数据解析----使用Google Gson解析Json数据

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. Google Gson解析Json数据应用实例

    转自:http://lixigao449778967.blog.163.com/blog/static/24985164201269105928783/ 1.需要的Jar包 1) Google Gso ...

  8. Android--------使用gson解析json文件

    ##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...

  9. $Java-json系列(一):用GSON解析Json格式数据

    GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...

随机推荐

  1. 第78讲:Type与Class实战详解

    今天来学习下type与class解析 让我们先来看看代码 import scala.reflect.runtime.universe._ class Sparktrait Hadoopobject F ...

  2. Lambda架构

    转载:https://blog.csdn.net/brucesea/article/details/45937875 1.Lambda架构背景介绍 Lambda架构是由Storm的作者Nathan M ...

  3. Scala_对象

    对象 单例对象 Scala并没有提供Java那样的静态方法或静态字段,但是,可以采用 object关键字实现单例对象,具备和Java静态方法同样的功能. 可以看出,单例对象的定义和类的定义很相似,明显 ...

  4. java基础知识-原码,反码,补码

    1.正数:原码,反码,补码:都一样. 2.负数:和正数的储存方式不同,负数都是以补码形式存储的. <1>负数的补码 把负数的原码除了符号位取反后再+1. <2>负数的原码 把对 ...

  5. delphi编写与调用DLL(delphi7下测试通过)

    http://blog.sina.com.cn/s/blog_4dbbf76f01000anz.html delphi编写DLL 下面在delphi中编写一个简单的dll,在该dll中只有一个max函 ...

  6. hdu 1.2.5

    #include<cstdio> #include<cstring> int main() { //freopen("input.txt","r& ...

  7. 分形之希尔伯特-皮亚诺(Hilbert-Peano)曲线

    1890年,意大利数学家皮亚诺(Peano G)发明能填满一个正方形的曲线,叫做皮亚诺曲线.后来,由希尔伯特作出了这条曲线,又名希尔伯特曲线.Hilbert-Peano曲线是一种分形图形,它可以画得无 ...

  8. Kali Linux渗透测试实战 2.2 操作系统指纹识别

    目录 2.2 操作系统指纹识别 2.2.1 Banner抓取 2.2.2 TCP 和 ICMP 常规指纹识别技术 TCP数据报格式 ICMP首部格式 TTL与TCP窗口大小 FIN探测 BOGUS f ...

  9. 转(C# 类似右键菜单弹出窗体)

    文章来自 https://www.cnblogs.com/ahdung/p/FloatLayerBase.html 每天进步一点点 新建类  FloatLayerBase 继承Form, 自己有点小改 ...

  10. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...