Methods for converting JSON strings directly into objects in Java (including multilayer list collections) _java

Source: Internet
Author: User

Classes to use: Net.sf.json.JSONObject

When using JSON, In addition to importing the Json-lib-2.2-jdk15.jar packages downloaded above the JSON Web site, you must have several other dependent packages: Commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmor Ph.jar,morph-1.0.1.jar

Here is the example code:

JSON conversion
Jsonobject jsonobj = Jsonobject.fromobject (jsonstrbody);

map<string, class> classmap = new hashmap<string, class> ();
Classmap.put ("Results", weatherbean_baidu_city.class);
Classmap.put ("index", weatherbean_baidu_city_index.class); 
Classmap.put ("Weather_data", weatherbean_baidu_city_weatherdata.class); 

Converts JSON to Weatherbean_baidu 
weatherbean_baidu weather = (Weatherbean_baidu) Jsonobject.tobean (Jsonobj, 
Weatherbean_baidu.class, classmap); 
System.out.println (Weather.getresults ());

Several Java class code to use:

 package com.lenovo.conference.entity.vo;
Import java.io.Serializable;

Import java.util.List; /** * Weather Bean * * @author shanhy */@SuppressWarnings ("Serial") public class Weatherbean_baidu implements Serializ Able {private String error;//error number private string status;//status value private string date;//date private List<weatherbean_ba
	idu_city> results;//City Weather Forecast collection (because multiple cities can be queried at one time) 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<weatherbean_baidu_city> GetResults () {return results;
	public void SetResults (list<weatherbean_baidu_city> results) {this.results = results; }

}
Package com.lenovo.conference.entity.vo;
Import java.io.Serializable;

Import java.util.List; /** * Weather Bean * * @author shanhy */@SuppressWarnings ("Serial") public class Weatherbean_baidu_city implements Ser ializable {private string currentcity;//city name private string pm25;//pm2.5 value private List<weatherbean_baidu_city_inde X> index;//Index Collection private list<weatherbean_baidu_city_weatherdata> weather_data;//days of the weather set 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<weatherbean_baidu_city_index> GetIndex () {return Index;
	public void Setindex (list<weatherbean_baidu_city_index> index) {this.index = index;
		Public list<weatherbean_baidu_city_weatherdata> Getweather_data () {return weather_data; } public void Setweather_data (list<weatherbean_baidu_city_weatherdata> weather_data) {this.weather_data = W
	Eather_data;
 }

}
Package com.lenovo.conference.entity.vo;

Import java.io.Serializable; /** * Weather Bean * * * @author Shanhy */@SuppressWarnings ("Serial") public class Weatherbean_baidu_city_weatherdata IM Plements Serializable {private string date;//date private string daypictureurl;//daytime weather picture private string Nightpictureu rl;//Evening Weather Picture private string weather;//weather private string wind;//wind direction private string temperature;//temperature public Weatherb
	Ean_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;

/**
 * Weather Bean
 * * 
 @author shanhy
 * 
 *
/@SuppressWarnings ("Serial") Public
class Weatherbean_baidu_city_index implements Serializable {

	private string title;//title
	private string zs;//comfort
	private String tipt;//index
	Summary private string des;//exponent Overview 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;
	}

}

Parse the corresponding JSON string in the example

{"Error": 0, "status": "Success", "date": "2015-01-15", "results": [{"currentcity": "Nanjing", "PM25": "The", "index": [{"title"] : "Dressing", "ZS": "Cooler", "tipt": "Dressing Index", "des": "suggest a thick coat and sweater and other clothing." The frail and infirm are suitable for coats, coats and sweaters. "},{" "title": "Car Wash", "ZS": "More Appropriate", "Tipt": "Car Wash Index", "des": "more suitable for car wash, the next day without rain, wind small, scrub a new car can maintain at least one day." "},{" "title": "Tourism", "ZS": "Appropriate", "Tipt": "Tourism Index", "des": "The weather is better, the temperature is slightly lower, will feel a little cooler, but also a good weather oh." Suitable for tourism, but do not miss the opportunity yo! "},{" title: "Cold", "ZS": "Less Hair", "tipt": "Cold Index", "des": "The meteorological conditions appropriate, no obvious cooling process, the incidence of cold is lower." "},{" title ":" Sports "," ZS ":" More Unfavorable "," Tipt ":" Sports Index "," des ":" Cloudy, and the cold weather, recommended that you do low intensity indoor sports, if you insist on outdoor sports, please choose the right sports and attention to keep warm. " "},{" title: "Ultraviolet Intensity", "ZS": "Weakest", "tipt": "UV Intensity Index", "des": "is a weak ultraviolet radiation weather, no special protection." If the long-term outdoors, it is recommended to rub SPF between 8-12 sunscreen skin care products. "}]," Weather_data ": [{" Date ":" Thursday January 15 (Real-time: 6 ℃) "," Daypictureurl ":" Yun_qi_img/yin.png "," Nightpictureurl ":" Yun_qi_ Img/duoyun.png "," Weather ":" Overcast to Cloudy "," Wind ":" North Breeze "," Temperature ":" 8 ~ 4 ℃ "},{" date ":" Friday "," Daypictureurl ":" yun_qi_img/ Duoyun.png "," Nightpictureurl ":" Yun_qi_img/qing.png "," Weather ":" Cloudy Turn Fine "," Wind ":" Northwest Wind 3-4 "," Temperature ":" 12 ~ 0 ℃ "},{ "Date": "Saturday", "Daypictureurl": "YUN_QI_IMG/QING.PNg "," Nightpictureurl ":" Yun_qi_img/duoyun.png "," Weather ":" Clear to Cloudy "," Wind ":" The Northeast 3-4 level "," Temperature ":" 8 ~ 0 ℃ "},{" date ": "Sunday", "Daypictureurl": "Yun_qi_img/qing.png", "Nightpictureurl": "Yun_qi_img/qing.png", "Weather": "Clear", "wind": "Westerly Breeze" , "Temperature": "10 ~ -1℃"}]},{"" currentcity ":" Xuzhou "," PM25 ":" 154 "," index ": [{" title ":" Dress "," ZS ":" Colder "," tipt ":" Clothing Index "," Des ":" suggest a thick coat and sweater and other clothing. The frail and infirm are suitable for coats, coats and sweaters. "},{" "title": "Car Wash", "ZS": "More Appropriate", "Tipt": "Car Wash Index", "des": "more suitable for car wash, the next day without rain, wind small, scrub a new car can maintain at least one day." "},{" "title": "Tourism", "ZS": "Appropriate", "Tipt": "Tourism Index", "des": "The weather is better, but the slightest will not affect your mood to travel." The temperature is suitable and has the breeze accompanying, is suitable for traveling. "},{" title ":" Cold "," ZS ":" More prone to hair "," tipt ":" Cold Index "," des ":" cooler weather, more prone to colds, please increase the appropriate clothing. " Weak friends should pay special attention to protection. "},{" title: "Sports", "ZS": "More Unfavorable", "Tipt": "Sports Index", "des": "The weather is better, but consider the cold weather, recommend you to carry out a variety of indoor sports, if in outdoor sports please pay attention to keep warm and prepare activities." "},{" title: "Ultraviolet Intensity", "ZS": "Weakest", "tipt": "UV Intensity Index", "des": "is a weak ultraviolet radiation weather, no special protection." If the long-term outdoors, it is recommended to rub SPF between 8-12 sunscreen skin care products. "}]," Weather_data ": [{" Date ":" Thursday January 15 (Real-time: 6 ℃) "," Daypictureurl ":" Yun_qi_img/duoyun.png "," Nightpictureurl ":" Yun_ Qi_img/duoyun.png "," Weather ":" Cloudy "," Wind ":" Southerly Breeze "," Temperature ":" 10 ~ 3 ℃ "},{" datE ":" Friday "," Daypictureurl ":" Yun_qi_img/duoyun.png "," Nightpictureurl ":" Yun_qi_img/duoyun.png "," Weather ":" Cloudy "," wind ":" The North wind 3-4 level "," Temperature ":" 11 ~ -4℃ "},{" date ":" Saturday "," Daypictureurl ":" Yun_qi_img/duoyun.png "," Nightpictureurl ":" Yun _qi_img/duoyun.png "," Weather ":" Cloudy "," Wind ":" Dongfeng Breeze "," Temperature ":" 6 ~ -4℃ "},{" date ":" Sunday "," Daypictureurl ":" Yun_qi_ Img/duoyun.png "," Nightpictureurl ":" Yun_qi_img/duoyun.png "," Weather ":" Cloudy "," Wind ":" Westerly level 3-4 "," Temperature ":" 11 ~ 1 ℃ "}]}]}

Above this Java JSON string directly converted to object method (including Multi-layer list collection) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.