Java 使用gson 解析 Json
2023-09-27 14:25:21 时间
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(); } } }
注意
文件路径相对路径是从工程根目录开始
相关文章
- fastJson java后台转换json格式数据
- java多线程执行问题
- Java ListIterator
- Java基础-内部类-为什么成员内部类可以无条件访问外部类
- JNI(Java Native Interface)
- java 中 json转 map 获取属性
- 利用JSONObject将json 字符串转换为java bean对象
- Java基础加强
- Json web token的简单实现 JAVA
- java追加文件
- JSON-JSON字符串转换成JSON对象、JSON对象数组、java实体类以及保存到List列表中
- java 权限 部分截图
- [Java IO]06_JSON操作
- 《Java核心技术 卷Ⅱ 高级特性(原书第10版)》一2.5.1 Path
- Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object
- jackson java转json hibernate懒加载造成的无限递归问题
- java 替换json字符串中间的引号保留两边的引号,避免json校验失败
- JAVA-J2ME移动开发实战教学
- Java数据库ResultSet转json实现
- json java simple-json
- Java 元注解
- java实现安全证书相关操作
- Java上位机开发(json处理)
- Java heap space 解决方法(转)
- 基于Java实现Json文件转换为Excel文件
- Java JSoup 请求Url地址及处理响应的JSON数据方法代码
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
- java对象怎么转json数组
- 19.JAVA-从文件中解析json、并写入Json文件(详解)
- Java经典实例:按字符颠倒字符串
- Java面向对象—继承
- Java中的JSON序列化和反序列化
- Java工作利器之常用工具类(四)——Json工具类,使用正则支持xml与json互转