使用到的类:net.sf.json.JSONObject

使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar

下面是例子代码:

// JSON转换

JSONObject jsonObj = JSONObject.fromObject(jsonStrBody);

Map classMap = new HashMap();

classMap.put("results", WeatherBean_Baidu_City.class);

classMap.put("index", WeatherBean_Baidu_City_Index.class);

classMap.put("weather_data", WeatherBean_Baidu_City_Weatherdata.class);

// 将JSON转换成WeatherBean_Baidu

WeatherBean_Baidu weather = (WeatherBean_Baidu) JSONObject.toBean(jsonObj,

WeatherBean_Baidu.class, classMap);

System.out.println(weather.getResults());

使用到的几个JAVA类代码:

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

import java.util.List;

/**

* 天气Bean

*

* @author SHANHY

*

*/

@SuppressWarnings("serial")

public class WeatherBean_Baidu implements Serializable {

private String error;//错误号

private String status;//状态值

private String date;//日期

private List results;//城市天气预报集合(因为一次可以查询多个城市)

public WeatherBean_Baidu() {

super();

}

public String getError() {

return error;

}

public void setError(String error) {

this.error = error;

}

public String getStatus() {

return status;

}

public void setStatus(String status) {

this.status = status;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public List getResults() {

return results;

}

public void setResults(List results) {

this.results = results;

}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

import java.util.List;

/**

* 天气Bean

*

* @author SHANHY

*

*/

@SuppressWarnings("serial")

public class WeatherBean_Baidu_City implements Serializable {

private String currentCity;//城市名称

private String pm25;//pm2.5值

private List index;//指数集合

private List weather_data;//几天的天气集合

public WeatherBean_Baidu_City() {

super();

}

public String getCurrentCity() {

return currentCity;

}

public void setCurrentCity(String currentCity) {

this.currentCity = currentCity;

}

public String getPm25() {

return pm25;

}

public void setPm25(String pm25) {

this.pm25 = pm25;

}

public List getIndex() {

return index;

}

public void setIndex(List index) {

this.index = index;

}

public List getWeather_data() {

return weather_data;

}

public void setWeather_data(

List weather_data) {

this.weather_data = weather_data;

}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

/**

* 天气Bean

*

* @author SHANHY

*

*/

@SuppressWarnings("serial")

public class WeatherBean_Baidu_City_Weatherdata implements Serializable {

private String date;// 日期

private String dayPictureUrl;// 白天的天气图片

private String nightPictureUrl;// 晚上的天气图片

private String weather;// 天气

private String wind;// 风向

private String temperature;// 温度

public WeatherBean_Baidu_City_Weatherdata() {

super();

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public String getDayPictureUrl() {

return dayPictureUrl;

}

public void setDayPictureUrl(String dayPictureUrl) {

this.dayPictureUrl = dayPictureUrl;

}

public String getNightPictureUrl() {

return nightPictureUrl;

}

public void setNightPictureUrl(String nightPictureUrl) {

this.nightPictureUrl = nightPictureUrl;

}

public String getWeather() {

return weather;

}

public void setWeather(String weather) {

this.weather = weather;

}

public String getWind() {

return wind;

}

public void setWind(String wind) {

this.wind = wind;

}

public String getTemperature() {

return temperature;

}

public void setTemperature(String temperature) {

this.temperature = temperature;

}

}

package com.lenovo.conference.entity.vo;

import java.io.Serializable;

/**

* 天气Bean

*

* @author SHANHY

*

*/

@SuppressWarnings("serial")

public class WeatherBean_Baidu_City_Index implements Serializable {

private String title;//标题

private String zs;//舒适度

private String tipt;//指数简述

private String des;//指数概述

public WeatherBean_Baidu_City_Index() {

super();

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getZs() {

return zs;

}

public void setZs(String zs) {

this.zs = zs;

}

public String getTipt() {

return tipt;

}

public void setTipt(String tipt) {

this.tipt = tipt;

}

public String getDes() {

return des;

}

public void setDes(String des) {

this.des = des;

}

}

例子中解析所对应的JSON字符串

{"error":0,"status":"success","date":"2015-01-15","results":[{"currentCity":"南京","pm25":"83","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,气温稍低,会感觉稍微有点凉,不过也是个好天气哦。适宜旅游,可不要错过机会呦!"},{"title":"感冒","zs":"少发","tipt":"感冒指数","des":"各项气象条件适宜,无明显降温过程,发生感冒机率较低。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"阴天,且天气寒冷,推荐您在室内进行低强度运动;若坚持户外运动,请选择合适的运动并注意保暖。"},{"title":"紫外线强度","zs":"最弱","tipt":"紫外线强度指数","des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}],"weather_data":[{"date":"周四 01月15日 (实时:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"阴转多云","wind":"北风微风","temperature":"8 ~ 4℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"多云转晴","wind":"西北风3-4级","temperature":"12 ~ 0℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴转多云","wind":"东北风3-4级","temperature":"8 ~ 0℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"西风微风","temperature":"10 ~ -1℃"}]},{"currentCity":"徐州","pm25":"154","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"天气较好,但考虑天气寒冷,推荐您进行各种室内运动,若在户外运动请注意保暖并做好准备活动。"},{"title":"紫外线强度","zs":"最弱","tipt":"紫外线强度指数","des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}],"weather_data":[{"date":"周四 01月15日 (实时:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"南风微风","temperature":"10 ~ 3℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"北风3-4级","temperature":"11 ~ -4℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东风微风","temperature":"6 ~ -4℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"西风3-4级","temperature":"11 ~ -1℃"}]}]}

以上这篇Java中Json字符串直接转换为对象的方法(包括多层List集合)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

多层json字符串转map_Java中Json字符串直接转换为对象的方法(包括多层List集合)相关推荐

  1. java字符串转json取集合_Java中Json字符串直接转换为对象的方法(包括多层List集合)...

    使用到的类:net.sf.json.JSONObject 使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-b ...

  2. java list 对象 json字符串_Java中Json字符串直接转换为对象的方法(包括多层List集合)...

    使用到的类:net.sf.json.JSONObject 使用JSON时,除了要导入JSON网站上面下载的json-lib-2.2-jdk15.jar包之外,还必须有其它几个依赖包:commons-b ...

  3. MATLAB 存放字符串(循环中调用字符串)

    MATLAB 存放字符串(循环中调用字符串) str=['asdfs';'sdadas';'qweqweqwe'];for i=1:3str(i,:) end 用分号隔开就可以调用了,比如在写图片标题 ...

  4. oracle数据存储过程 中的循环 for 拼接字符串,oracle存储过程中使用字符串拼接

    1.使用拼接符号"||" v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAcc ...

  5. 【错误记录】Groovy 闭包使用报错 ( 闭包中不能直接使用外部对象的方法 | 需要先设置 delegate 代理 )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 Groovy 中的 Closure 闭包中 , 直接调用外部对象的方法 , 会报错 ; class Test {def fun() {print ...

  6. android studio 自动生成对象,在Android Studio中快速导入可绘制对象的方法?

    在Android Studio中快速导入可绘制对象的方法? 在Android项目中的某个时候,您将需要在res/drawable目录中导入一些可绘制对象-工具栏图标,图像,UI功能. 大多数情况下,您 ...

  7. android集合方法,android中intent传递list或者对象的方法

    本文实例讲述了android中intent传递list或者对象的方法.分享给大家供大家参考.具体实现方法如下: 方法一: 如果单纯的传递List 或者List的话 就可以直接使用 代码如下: inte ...

  8. python创建对象_对Python中9种生成新对象的方法总结

    先定义一个类: class Point: def __init__(self, x, y): self.x = x self.y = y 下面我们使用9种方法来生成新的对象: point1 = Poi ...

  9. java json 易用_Java中 Json的使用

    Java JSON 本章节我们将为大家介绍如何在 Java 语言中使用 JSON. 类库选择 Java中并没有内置JSON的解析,因此使用JSON需要借助第三方类库. 下面是几个常用的 JSON 解析 ...

最新文章

  1. java card applet_可多选的javacard applet | 学步园
  2. c++队列指针 结构体指针
  3. java中的list时间排序
  4. python进阶九_网络编程
  5. docker 查看已安装容器_WIN7下安装Docker容器
  6. aop阻止方法运行_Spring AOP无法拦截内部方法调用
  7. redis强一致性_分布式架构一定要有Redis吗?Redis的常见问题我都帮你解答了
  8. Python数据结构与算法(二)--timeit模块
  9. 分享一个入门级实操项目,思路非常棒!
  10. app怎么调用mysql数据_手机app怎么调用php操作数据库的接口?
  11. 产生滚动条时JPanel的大小发生变化
  12. 经典机器学习系列(三)【线性模型与广义线性模型】
  13. ROS2 Galactic teb_local_planner
  14. 外螺纹对照表_美制螺纹内外径对照表unf
  15. 跨平台局域网文件传输工具——Dukto R5
  16. android支付宝免密支付功能,手机支付宝如何将免密支付功能关闭掉
  17. 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。剑指Offer(32)
  18. ArcMap无法启动解决方法
  19. App逆向|一个案例教你如何进行APP逆向
  20. Structure of a Google Docs document 谷歌文档的结构

热门文章

  1. ABAP的两种OOALV实现方法 第二种画屏幕
  2. 解决GitHub打不开(FastGithub,支持MAC)
  3. 多项式最小二乘拟合算法实现
  4. 04UEc++【打飞艇:无人机发射胡萝卜】
  5. Android.mk语法解释
  6. 弹出html对话框样式,对话框或弹出自定义样式的3种方法
  7. 计算机文件是怎么存储的,电脑中重要的文档如何保存?有哪些的方法呢?
  8. uni-app 简单引入iconfont示例(阿里妈妈)
  9. 再说说医疗服务产业的新机会
  10. 论文学习随记:Unsupervised 3D Shape Completion through GAN Inversion