With the use of third-party APIs, sometimes JSON data is obtained from the network, so how will we parse the JSON data?
The following small series will be the following points for the JSON explanation
1. What is JSON? (http://www.json.org/)
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It's easy for machines to parse and generate. It is based in a subset of the JavaScript programming Language, Standard ECMA-262 3rd Edit Ion-december 1999. JSON is a text format, which is completely language independent but uses conventions that's familiar to programmers of the C-family of languages, including C, C + +, C #, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal Data-interchange language.
(image from: http://www.cnblogs.com/xiaoluo501395377/p/3446605.html )
2.Json Data types
2-1.json Object
2-2.json Array
The difference between Ps:jsonobject and Jsonarray
(JSON array)
(JSON array)
3. Parsing JSON data (Gson used by small series for parsing of JSON data)
3-1 "Analysis of Jsonobject"
Here is a JSON file:
{"ResultCode": "$", "Reason": "successed!", "result": {"SK": {"temp": "+", "WI Nd_direction ":" Southwest Wind "," wind_strength ":" Level 2 "," humidity ":" 51% "," Time ":" 10:11 "}, "Today": {"Temperature": "16℃~27℃", "Weather": "Overcast Turn cloudy", "weather_id": { "FA": "Southwest Wind", "FB": "Week"}, "Wind": "3-4-level", "a", "Thursday", "City": "Binzhou", "date_y": "June 04, 2015", "Dressing_index": "Comfortable", "dressing_advice": "Recommended Long-sleeved T-shirts, shirts and trousers and other clothing. The frail and infirm are advised to knit long-sleeved shirts, vests and trousers. "," Uv_index ":" Weakest "," Comfort_index ":" "," Wash_index ":" More Appropriate "," Travel_index ": "", "Exercise_index": "More Appropriate", "Drying_index": ""}, "Future": [{ "Temperature": "16℃~27℃", "Weather": "Overcast Turn cloudy", "weather_id": { "FA": "Up", "FB": "Southwest Wind"}, "Wind": "Level 3-4", "Week": "Thursday", "date": "20150604"}, {"Temperature": "20℃~32℃", "Weather": "Cloudy to Clear", "weather_id": {"FA": "", "FB": "00" }, "Wind": "Westerly level 3-4", "Week": "Friday", "date": "20150605"}, {"Temperature": "23℃~35℃", "Weather": "Cloudy", "weather_id": { "FA": "Southwest Wind", "FB": "Up"}, "Wind": "Level 3-4", "Week": "Saturday", "date": "20150606"}, {"Temperature": "20℃~33℃", "Weather": "Cloudy", "weather_id": {"FA": "" "," FB ":" 01 " }, "Wind": "Northern Breeze", "Week": "Sunday", "date": "20150607"}, { "Temperature": "22℃~34℃", "Weather": "Cloudy", "weather_id": {"FA" : "Southwest Wind", "FB": "Week"}, "Wind": "3-4", "the" "Monday", "Date": "20150608"}, {"Temperature": "22℃~33℃", "Weath Er ":" Yin "," weather_id ": {" FA ":" The "," FB ":" 02 "}, "Wind": "Southwest Wind Level 3-4", "Week": "Tuesday", "date": "20150609"}, {"Temperature": "22℃~33℃", "Weather": "Cloudy", "weather_id": { "FA": "The", "FB": "Week"}, "Wind": "Southerly level 3-4", "the" Star ":" Period three "," DatE ":" 20150610 "}]}," Error_code ": 0}
We parse (parse part):
Package Cn.edu.bzu.json;import Java.io.filenotfoundexception;import Java.io.filereader;import Com.google.gson.jsonarray;import Com.google.gson.jsonioexception;import Com.google.gson.jsonobject;import Com.google.gson.jsonparser;import Com.google.gson.jsonsyntaxexception;public class Read {public static void main (Stri Ng args[]) {Jsonparser parse =new jsonparser (); Create a JSON parser try {jsonobject json= (jsonobject) parse.parse (New FileReader ("Weather.json")); Create the Jsonobject object System.out.println ("ResultCode:" +json.get ("ResultCode"). Getasint ()); Convert the JSON data to the data System.out.println of type int ("Reason:" +json.get ("Reason"). Getasstring ()); Converts the JSON data to a string-type data jsonobject result=json.get ("result"). Getasjsonobject (); Jsonobject Today=result.get ("Today"). Getasjsonobject (); System.out.println ("Temperature:" +today.get ("Temperature"). Getasstring ()); System.out.println ("Weather:" +today.get ("Weather"). Getasstring ()); } catch (Jsonioexception e) {e.printstacktrace (); } catch (Jsonsyntaxexception e) {e.printstacktrace (); } catch (FileNotFoundException e) {e.printstacktrace (); } }}
Output Result:
3-2 "Analysis of Jsonarray"
Here is a JSON file
{ "cat": "It", "language": [ {"id": 1, "IDE": "Eclipse", "name": Java}, {"id": 2, "IDE": "XCode", "name": "Swift"}, {"id": 3, "IDE": "Visual Stdio", "name": "C #"} ] ,"Pop":true}
We do the parsing:
Package Cn.edu.bzu.json;import Java.io.filenotfoundexception;import Java.io.filereader;import Com.google.gson.jsonarray;import Com.google.gson.jsonioexception;import Com.google.gson.jsonobject;import Com.google.gson.jsonparser;import Com.google.gson.jsonsyntaxexception;public class ReadJSON {public static void main ( String args[]) {try {jsonparser parser=new jsonparser (); Create a JSON parser Jsonobject object= (jsonobject) parser.parse (New FileReader ("Test.json")); Create the Jsonobject object System.out.println ("cat=" +object.get ("Cat"). getasstring ()); Converts the JSON data to a string-type data System.out.println ("pop=" +object.get ("Pop"). Getasboolean ()); Convert the JSON data to a Boolean data Jsonarray Array=object.get ("language"). Getasjsonarray (); Gets the array for the JSON for (int i=0;i<array.size (); i++) {System.out.println ("---------------"); Jsonobject Subobject=array.get (i). Getasjsonobject (); SysteM.out.println ("id=" +subobject.get ("id"). getasint ()); System.out.println ("Name=" +subobject.get ("name"). getasstring ()); System.out.println ("ide=" +subobject.get ("IDE"). getasstring ()); }} catch (Jsonioexception e) {e.printstacktrace (); } catch (Jsonsyntaxexception e) {e.printstacktrace (); } catch (FileNotFoundException e) {e.printstacktrace (); } }}
Output Result:
3-3 "Analysis"
We parse through Gson, so we need to import Gson.jar before use
When parsing JSON data,
1. Need to create Gson parser
2. Create a Jsonobject object
3. Convert the JSON data to the appropriate data
4. Source code Download:
Https://github.com/monsterLin/TestReadJSON
Java parsing JSON data