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());
}
相关推荐
阿拉斯攀登6 分钟前
深入微服务配置中心:Nacos注册中心的实操细节
java·微服务·云原生·springcloud
贩卖黄昏的熊7 分钟前
typescript 快速入门
开发语言·前端·javascript·typescript·ecmascript·es6
f***24117 分钟前
springboot系列--自动配置原理
java·spring boot·后端
一 乐27 分钟前
水果销售|基于springboot + vue水果商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
JIngJaneIL28 分钟前
校园任务平台|校园社区系统|基于java+vue的校园悬赏任务平台系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园任务平台
三省同学31 分钟前
SpringBoot 项目LOG_PATH_IS_UNDEFINED问题完整解决方案
java·spring boot·后端
阿蔹31 分钟前
抓包工具Charles——介绍、篡改数据、弱网环境测试
java·自动化·抓包·charles
剪一朵云爱着33 分钟前
PAT 1164 Good in C
c语言·开发语言
i***683237 分钟前
【MyBatis】spring整合mybatis教程(详细易懂)
java·spring·mybatis
小马爱打代码1 小时前
Spring AI:ChatMemory 实现聊天记忆功能
java·人工智能·spring