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. java8 日期时间解析与转换

    Instant now = Instant.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM ...

  2. python 生成 pptx 分析报告的工具包:reportgen

    python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...

  3. Tomcat系列(10)——Tomcat主要设计模式5种(外观,责任链,观察者,模板方法,命令模式)

    核心部分 外观模式: RequestFacade应用门面模式(facade)来封装HttpServletRequest. 观察者模式: 事件监听机制,控制组件生命周期的 Lifecycle .Serv ...

  4. IDEA+Maven+Spring MVC HelloWorld示例

    用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...

  5. linxu上安装mongodb3.6实战

    根据linux 版本到官网下载对应mongodb版本 查看服务器版本:cat /proc/version 查看linux发行版本:cat /etc/redhat-release 我用的阿里云服务器,对 ...

  6. 【C++笔记】析构函数(destructor)

    “析构函数”是构造函数的反向函数. 在销毁(释放)对象时将调用它们. 通过在类名前面放置一个波形符 (~) 将函数指定为类的析构函数.   声明析构函数   析构函数是具有与类相同的名称但前面是波形符 ...

  7. AE二次开发中几个功能速成归纳(符号设计器、创建要素、图形编辑、属性表编辑、缓冲区分析)

    /* * 实习课上讲进阶功能所用文档,因为赶时间从网上抄抄改改,凑合能用,记录一下以备个人后用. * * ----------------------------------------------- ...

  8. ASP.NET MVC上传文件

    最近参考网络资料,学习了ASP.NET MVC如何上传文件.最基本的,没有用jQuery等技术. 1.定义Model public class TestModel    {        [Displ ...

  9. 安装hue及hadoop和hive整合

    环境: centos7 jdk1.8.0_111 Hadoop 2.7.3 Hive1.2.2 hue-3.10.0 Hue安装: 1.下载hue-3.10.0.tgz: https://dl.dro ...

  10. Android logcat lines missing原因分析

    当出现类似如下错误日志时: 2019-04-14 17:51:14.506 10189-10189/com.ss.android.ex.parent D/GGK: no WonderfulVideo ...