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());
}
相关推荐
2201_757830871 天前
全局异常处理器
java
知远同学1 天前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力1 天前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo1 天前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
Coder_Boy_1 天前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
kylezhao20191 天前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子1 天前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
Coder_Boy_1 天前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
热爱专研AI的学妹1 天前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
Mr_Chenph1 天前
Miniconda3在Windows11上和本地Python共生
开发语言·python·miniconda3