Parsing Json nested data with Gson in Android Development
Gson parses complex json data
Another method to parse json data is described here through Gson resolution. I will not introduce a complicated json data for parsing relatively simple json data, the following is a json data to be parsed:
[Java]
- String json = {a: 100, B: [{b1: B _value1, b2: B _value2}, {b1: B _value1, b2: B _value2}], c: {c1: c_value1, c2: c_value2 }}
If you use the combination of JsonObject and JsonArray, it can be parsed, but it is troublesome to parse it. If you use Gson for parsing, it is easier. First, we need to define a serialized Bean, the internal class is used here, which is easier to see.
First, we need to define a serialized Bean. Here we use the internal class form, which looks clearer:
[Java]
- Public class JsonBean {
- Public String;
- Public ListB;
- Public C c;
-
- Public static class B {
-
- Public String b1;
-
- Public String b2;
- }
-
- Public static class C {
- Public String c1;
- Public String c2;
- }
- }
Most of the time, you do not know how to define this Bean. Here, you need to pay attention to the following points:
1. Internal Nested classes must be static, otherwise parsing will fail;
2. the attribute names in the class must be exactly the same as the keys in the Json field;
3. The part enclosed in [] is a List, which is defined as public List.B, and the nested {} is defined as public C c,
You can check the Json string for details. If you do not understand, you can communicate with each other. I am also a beginner in development!
[Java]View plaincopy
- Gson gson = new Gson ();
- Java. lang. reflect. Type type = new TypeToken () {}. GetType ();
- JsonBean jsonBean = gson. fromJson (json, type );
Then it's easy to get the data, just retrieve it in jsonBean!
If the Json to be parsed is nested in many layers, you can also define a Bean with many layers of internal classes. You need to carefully compare the Json fields to define the Bean.
The following describes how to parse complex json data using a specific column in Gson mode.
1. The data to be parsed is in the following format:
{
Error: 0,
Status: success,
Date: 2014-05-10,
Results :[
{
CurrentCity: Nanjing,
Weather_data :[
{
Date: Saturday (today, real-time: 19 ℃ ),
DayPictureUrl: http://api.map.baidu.com/images/weather/day/dayu.png,
NightPictureUrl: http://api.map.baidu.com/images/weather/night/dayu.png,
Weather: heavy rain,
Wind: southeast wind 5-6,
Temperature: 18 ℃
},
{
Date: Sunday,
DayPictureUrl: http://api.map.baidu.com/images/weather/day/zhenyu.png,
NightPictureUrl: http://api.map.baidu.com/images/weather/night/duoyun.png,
Weather: Shower turns cloudy,
Wind: northwest wind 4-5,
Temperature: 21 ~ 14 ℃
}
]
}
]
}
2. You must define the following javaBean data.
Status. java
[Java]View plaincopy
- Public class Status
- {
- Private String error;
- Private String status;
- Private String date;
- Private List Results;
- 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;
- }
- @ Override
- Public String toString ()
- {
- Return Status [error = + error +, status = + status
- +, Date = + date +, results = + results +];
- } Results. java
[Java]View plaincopy
- Public class Results
- {
- Private String currentCity;
- Private List Weather_data;
- Public String getCurrentCity ()
- {
- Return currentCity;
- }
- Public void setCurrentCity (String currentCity)
- {
- This. currentCity = currentCity;
- }
- Public List GetWeather_data ()
- {
- Return weather_data;
- }
- Public void setWeather_data (List Weather_data)
- {
- This. weather_data = weather_data;
- }
- @ Override
- Public String toString ()
- {
- Return Results [currentCity = + currentCity +, weather_data =
- + Weather_data +];
- }
Weather. java
[Java]View plaincopy
- Public class Weather {
- Private String date;
- Private String dayPictureUrl;
- Private String nightPictureUrl;
- Private String weather;
- Private String wind;
- Private String temperature;
- 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. Maid = maid;
- }
- 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;
- }
- @ Override
- Public String toString (){
- Return Weather [date = + date +, dayPictureUrl =
- + DayPictureUrl +, nightPictureUrl =
- + NightPictureUrl +, weather = + weather
- +, Wind = + wind +, temperature = + temperature
- +];
- }
- Then the specific javabean definition will parse the data. below is my parsing data class
[Java]View plaincopy
- Public class MainActivity extends Activity
- {
- Private Button tojson;
- RequestQueue mQueue;
- StringRequest stringRequest;
- Gson gson;
- String str;
-
- @ Override
- Protected void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
-
- Tojson = (Button) findViewById (R. id. tojson );
- Gson = new Gson ();
-
- MQueue = Volley. newRequestQueue (MainActivity. this );
- // Http: // 10.19.20.12/upgrade/test.txt is the json data used for testing.
- StringRequest = new StringRequest (http: // 10.19.20.12/upgrade/test.txt,
- New Response. Listener ()
- {
- @ Override
- Public void onResponse (String response)
- {
- Log. d (TAG, response );
- System. out. println (response = + response );
- Status status = gson. fromJson (response, Status. class );
- System. out. println (status = + status );
- System. out. println (-------------------------------------);
- List Result = status. getResults ();
- System. out. println (result = + result );
-
- }
- },
- New Response. ErrorListener ()
- {
- @ Override
- Public void onErrorResponse (VolleyError error)
- {
- Log. e (TAG, error. getMessage (), error );
- }
-
- });
-
- Tojson. setOnClickListener (new OnClickListener ()
- {
- @ Override
- Public void onClick (View v)
- {
- MQueue. add (stringRequest );
- }
- });
- }
-
-
-
- }
- The above RequestQueue is the use of the open-source network library Volley. If you are not familiar with the use of this library, refer
You can read it carefully. Reprinted: http://blog.csdn.net/tkwxty/article/details/34474501