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为

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

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

  1. Java解析json(二):jackson

    Java解析json(二):jackson   官方参考 Jackson Home Page:https://github.com/FasterXML/jackson Jackson Wiki:htt ...

  2. Java解析json字符串和json数组

    Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...

  3. java解析Json字符串之懒人大法

    面对Java解析Json字符串的需求,有很多开源工具供我们选择,如google的Gson.阿里巴巴的fastJson.在网上能找到大量的文章讲解这些工具的使用方法.我也是参考这些文章封装了自己的Jso ...

  4. Introduction to Structured Data json的2种形式 JAVA解析JSON数据 - JsonArray JsonObject

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  5. java解析json数组

      java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...

  6. Java解析JSON文件的方法

    http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...

  7. java解析json字符串详解(两种方法)

    一.使用JSONObject来解析JSON数据官方提供的,所以不需要导入第三方jar包:直接上代码,如下 private void parseJSONWithJSONObject(String Jso ...

  8. java解析json

    1:下载另外一个Java的小包就可以了: http://www.JSON.org/java/json_simple.zip 里面有源码和文档例题和编程的lib包:编程只需要json_simple.ja ...

  9. java 解析json的问题

    本文转载自http://chriszz.sinaapp.com/?p=392 Json就是Javascript notation,可以替代XML,用做数据交互. Json的两种基本表示形式,可以用自动 ...

随机推荐

  1. Matlab安装完成后,出现错误licensing error:-8523的解决方法

    Matlab2018安装成功后,打开出现licensing error:-8523解决方法 https://blog.csdn.net/qq_41634276/article/details/8000 ...

  2. Qt: usb热插拔检测(windows);

    Qt提供了QAbstractNativeEventFilter来实现本地时间得过滤,通过对本地事件的检测,判断usb热插拔:(这里是windows 的例子); 首先,以QWidget, QAbstra ...

  3. jenkins_jmeter配置

    echo "job begin" date export current_time=`date "+%Y%m%d_%H%M%S"` mkdir -p ${WOR ...

  4. vue全局变量的使用

    新建一个VUE文件,声明一个变量,并且把它export. 在main.js中引入,并声明. 在其他地方使用,直接this就可以了.

  5. 题解 P3871 【[TJOI2010]中位数】

    orz各位大佬,题解太强了,主席树,堆,线段树,splay,还有暴力,太巨了.所以我用的是fhq treap(好像更高级).算了. 反正都是平衡树,这道题就是动态求中位数,不会做的同学可以先做弱化版P ...

  6. setLocale(java.util.Locale), setCharacterEncoding(java.lang.String),setContentType(java.lang.String type)

    对于setCharacterEncoding(java.lang.String),这个方法是javax.servlet.ServletRequest和javax.servlet.ServletResp ...

  7. python整数与IP地址转换

    python整数与IP地址转换 [转] 我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import ...

  8. python面试题一个字符串是否由重复的子字符串组成

    一,给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 输入: "abab" 输出: True 解释: 可由 ...

  9. 出现: object() takes no parameters 之后应该如何修改

    这个错误花费了很多的时间去解决,包括重写代码也无济于事. 因为粗心,浪费了很多的时间在这个上面,特写此博客来记录,也希望朋友们不要粗心,特别是初学者. 接下来进入正文: 当在写self.XXX  下方 ...

  10. Python3 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下

    #!/usr/bin/env python3 # -*- coding:utf8 -*- # @TIME :2018/9/17 9:02 # @Author:dazhan # @File :copyf ...