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());
}
相关推荐
MediaTea1 小时前
Python IDE:Spyder
开发语言·ide·python
不枯石1 小时前
Matlab通过GUI实现点云的均值滤波(附最简版)
开发语言·图像处理·算法·计算机视觉·matlab·均值算法
不枯石2 小时前
Matlab通过GUI实现点云的双边(Bilateral)滤波(附最简版)
开发语言·图像处理·算法·计算机视觉·matlab
ccccczy_2 小时前
Spring Security 深度解读:JWT 无状态认证与权限控制实现细节
java·spring security·jwt·authentication·authorization·securityfilterchain·onceperrequestfilter
Lin_Aries_04212 小时前
容器化 Tomcat 应用程序
java·linux·运维·docker·容器·tomcat
sheji34162 小时前
【开题答辩全过程】以 springboot高校社团管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
二十雨辰2 小时前
vite如何处理项目中的资源
开发语言·javascript
zzywxc7873 小时前
大模型落地实践指南:从技术路径到企业级解决方案
java·人工智能·python·microsoft·golang·prompt
相与还3 小时前
IDEA+SpringBoot实现远程DEBUG到本机
java·spring boot·intellij-idea
聆风吟º3 小时前
远程录制新体验:Bililive-go与cpolar的无缝协作
开发语言·后端·golang