The previous few days have been doing a simple Android simple weather forecast, but there are some problems in parsing the JSON, because there was no previous exposure to JSON, so this time to do the first look at the JSON knowledge:
First, the server-side project to import the JSON jar package and JSON depends on the jar package to the Builtpath path (these can be downloaded to json-lib official website: http://json-lib.sourceforge.net/)
The JSON parsing part of Android is under package Org.json, mainly in the following categories:
Jsonobject : Can be thought of as a JSON object, which is the basic unit in the system for json definition, which contains a pair of ( Key/value) value. It lang= "en-us" to external tostring () method output value key and value are colon ":" delimited). Its operation format for internal jsonobject instance, referencing the internal The put () method adds a value: New Jsonobject (). Put ("JSON", "Hello, world!"), between key and value is a comma Span lang= "en-us" > "," delimited. value types include: boolean, jsonarray, Jsonobject, number, string or default jsonobject.null object.
Jsonarray: It represents an ordered set of values. Converting it to a string output (toString) is performed in the form of a square bracket, separated by a comma "," (for example: [Value1,value2,value3], you can personally use the short code to understand its format more intuitively). The inside of this class also has query behavior, both get () and opt () can return the specified value through the index index, and the put () method is used to add or replace values. Similarly, the value type of this class can include: Boolean, Jsonarray, Jsonobject, number, string, or Default value Jsonobject.null object.
JSON is mainly the conversion between JSON object and JSON array;
Here we begin to parse the data of the aggregated data weather forecast (I resolved the weather in a place the next day):
Jsonobject jsonobject= New Jsonobject (jsondata); Jsonobject result = Jsonobject.getjsonobject ("result"); Jsonobject data = result.getjsonobject ("data"); String weather = data.getstring ("weather"); Jsonarray Jsonarray = new Jsonarray (weather); Jsonobject jsonobject = jsonarray.getjsonobject (1); Jsonobject Info2 = jsonobject.getjsonobject ("info"); Date = jsonobject.getstring ("date"); info = info2.getstring ("Day"); String [] stringarr= info.split (","); Note that delimiters are required to translate the drops ... temp1 = Stringarr[0];weather1 = STRINGARR[1];TEMP2 = stringarr[2];
Aggregated data for Android weather forecast JSON read