JSON在java中的使用

方法:

JSON格式:{key:value,key:value,...} {},{},...

JSONObject JSON格式对象

JSONArray 存储多个JSON格式的对象

JSON:提供了javabean <--> JSONString

代码:

java 复制代码
Weather w1=new Weather();
w1.setCity("西安");
w1.setWeather("晴朗");
w1.setTemperature("39°C");
w1.setWind("无风");

//1.toJSONString()  将javaBean转JSON格式字符串
String json=JSON.toJSONString(w1);
System.out.println(json);
java 复制代码
String jsonStr="{\"city\":\"岷县\",\"temperature\":\"19°C\",\"weather\":\"晴朗\",\"wind\":\"二级\"}";

//将JSON格式的字符串转为JSON格式的对象
JSONObject jsonObject=JSON.parseObject(jsonStr);
System.out.println(jsonObject.get("city"));

//将JSON格式的字符串转成javaBean对象
Weather weather=JSON.parseObject(jsonStr,Weather.class);
System.out.println(weather);


Weather w2=JSON.parseObject(jsonStr,new TypeReference<Weather>(){});
System.out.println(w2);
java 复制代码
String jsonStr="[{\"city\":\"西安\",\"temperature\":\"39°C\",\"weather\":\"晴朗\",\"wind\":\"无风\"},{\"city\":\"岷县\",\"temperature\":\"18°C\",\"weather\":\"晴朗\",\"wind\":\"微风\"}]";
JSONArray jsonArray= JSON.parseArray(jsonStr);
for(int i=0;i<jsonArray.size();i++){
        JSONObject jsonObject=jsonArray.getJSONObject(i);
        Object city=jsonObject.get("city");
        System.out.println(city);
}

System.out.println("====================");
//转List集合
List<Weather> list=JSON.parseArray(jsonStr, Weather.class);
for(Weather weather:list){
      System.out.println(weather.getCity()+":"+weather.getTemperature());
}

System.out.println("============================");
List<Weather> weathers=JSON.parseObject(jsonStr,new TypeReference<List<Weather>>(){});
for(Weather weather:weathers){
      System.out.println(weather.getCity()+":"+weather.getTemperature());
}
java 复制代码
String jsonStr="{\"城市1\":{\"city\":\"西安\",\"temperature\":\"39°C\",\"weather\":\"晴朗\",\"wind\":\"无风\"},\"城市2\":{\"city\":\"岷县\",\"temperature\":\"18°C\",\"weather\":\"晴朗\",\"wind\":\"微风\"}}";
JSONObject obj=JSONObject.parseObject(jsonStr);
JSONObject jsonObject=obj.getJSONObject("城市2");
String city=jsonObject.getString("city");

System.out.println("city:"+city);

//getObject()
Weather weather1=obj.getObject("城市1",Weather.class);
System.out.println(weather1.getCity());


System.out.println("=================");
Map<String,Weather> map= JSON.parseObject(jsonStr,new TypeReference<Map<String,Weather>>(){});

//获取所有的值遍历
Collection<Weather> weathers=map.values();
Iterator<Weather> iterator=weathers.iterator();
while(iterator.hasNext()){
    System.out.println(iterator.next().getCity());
}
相关推荐
论迹复利37 分钟前
FreeRTOS 在 RISC-V 上是如何“点火“的 —— 从 main() 到第一个任务的完整链路
java·开发语言·risc-v
张宇Joaquin1 小时前
鲲鹏统一并行加速库KUPL--众核并行能力介绍
java·开发语言·网络
736c1 小时前
C语言-数组 07
c语言·开发语言
宸津-代码粉碎机2 小时前
AI攻防战升级!基于Spring AI构建Java应用自动免疫安全体系
java·大数据·开发语言·人工智能·python·安全·spring
2601_963749102 小时前
越华环保集团数字化污水治理:端边云采集架构与平台对接实现
java·大数据·架构
xcLeigh3 小时前
Go入门:整数类型的溢出与安全边界
java·安全·golang
LorryJovens3 小时前
【LAAP科研】CEN因果等价网络理论---从双缝干涉到因果等价
开发语言·php
源码宝3 小时前
SpringBoot+Vue2 诊所门诊管理系统,完整前后端源码,可直接部署上线
java·源码·his系统·门诊系统·程序代码·诊所系统·门诊his
yychen_java3 小时前
九:Text-to-SQL 智能数据查询与 Human-in-the-Loop 人机协作
java·人工智能·架构
Brilliantwxx4 小时前
【Linux】 进程(10) 进程控制深度解析:创建、终止与等待
linux·运维·服务器·c语言·开发语言·网络