很多时候大家都是不知道这个Bean是该怎么定义,这里面需要注意几点:
1、内部嵌套的类必须是static的,要不然解析会出错;
2、类里面的属性名必须跟Json字段里面的Key是一模一样的;
3、内部嵌套的用[]括起来的部分是一个List,所以定义为 public List b,而只用{}嵌套的就定义为 public C c,
具体的大家对照Json字符串看看就明白了,不明白的我们可以互相交流,本人也是开发新手!
Gson gson =newGson();
java.lang.reflect.Type type = newTypeToken() {}.getType();
JsonBean jsonBean = gson.fromJson(json, type);
然后想拿数据就很简单啦,直接在jsonBean里面取就可以了!
如果需要解析的Json嵌套了很多层,同样可以可以定义一个嵌套很多层内部类的Bean,需要细心的对照Json字段来定义哦。
下面我将以一个具体的列子来说明通过Gson方式解析复杂的json数据
1.将要解析的数据如下面的格式
{
"error": 0,
"status": "success",
"date": "2014-05-10",
"results": [
{
"currentCity": "南京",
"weather_data": [
{
"date": "周六(今天, 实时:19℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/dayu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/dayu.png",
"weather": "大雨",
"wind": "东南风5-6级",
"temperature": "18℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather": "阵雨转多云",
"wind": "西北风4-5级",
"temperature": "21 ~ 14℃"
}
]
}
]
}
2.必须定义如下一些的javaBean数据
Status.java
publicclassStatus
{
privateString error;
privateString status;
privateString date;
privateList results;
publicString getError()
{
returnerror;
}
publicvoidsetError(String error)
{
this.error = error;
}
publicString getStatus()
{
returnstatus;
}
publicvoidsetStatus(String status)
{
this.status = status;
}
publicString getDate()
{
returndate;
}
publicvoidsetDate(String date)
{
this.date = date;
}
publicList getResults()
{
returnresults;
}
publicvoidsetResults(List results)
{
this.results = results;
}
@Override
publicString toString()
{
return"Status [error="+ error +", status="+ status
+ ", date="+ date +", results="+ results +"]";
}
Results.java
publicclassResults
{
privateString currentCity;
privateList weather_data;
publicString getCurrentCity()
{
returncurrentCity;
}
publicvoidsetCurrentCity(String currentCity)
{
this.currentCity = currentCity;
}
publicList getWeather_data()
{
returnweather_data;
}
publicvoidsetWeather_data(List weather_data)
{
this.weather_data = weather_data;
}
@Override
publicString toString()
{
return"Results [currentCity="+ currentCity +", weather_data="
+ weather_data + "]";
}
Weather.java
publicclassWeather {
privateString date;
privateString dayPictureUrl;
privateString nightPictureUrl;
privateString weather;
privateString wind;
privateString temperature;
publicString getDate() {
returndate;
}
publicvoidsetDate(String date) {
this.date = date;
}
publicString getDayPictureUrl() {
returndayPictureUrl;
}
publicvoidsetDayPictureUrl(String dayPictureUrl) {
this.dayPictureUrl = dayPictureUrl;
}
publicString getNightPictureUrl() {
returnnightPictureUrl;
}
publicvoidsetNightPictureUrl(String nightPictureUrl) {
this.nightPictureUrl = nightPictureUrl;
}
publicString getWeather() {
returnweather;
}
publicvoidsetWeather(String weather) {
this.weather = weather;
}
publicString getWind() {
returnwind;
}
publicvoidsetWind(String wind) {
this.wind = wind;
}
publicString getTemperature() {
returntemperature;
}
publicvoidsetTemperature(String temperature) {
this.temperature = temperature;
}
@Override
publicString toString() {
return"Weather [date="+ date +", dayPictureUrl="
+ dayPictureUrl + ", nightPictureUrl="
+ nightPictureUrl + ", weather="+ weather
+ ", wind="+ wind +", temperature="+ temperature
+ "]";
}
然后具体的javabean定义好了就将解析数据了,下面就是我的解析数据类
publicclassMainActivityextendsActivity
{
privateButton tojson;
RequestQueue mQueue;
StringRequest stringRequest;
Gson gson;
String str;
@Override
protectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tojson = (Button)findViewById(R.id.tojson);
gson = newGson();
mQueue = Volley.newRequestQueue(MainActivity.this);
//http://10.19.20.12/upgrade/test.txt是测试使用的json数据
stringRequest = newStringRequest("http://10.19.20.12/upgrade/test.txt",
newResponse.Listener()
{
@Override
publicvoidonResponse(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);
}
},
newResponse.ErrorListener()
{
@Override
publicvoidonErrorResponse(VolleyError error)
{
Log.e("TAG", error.getMessage(), error);
}
});
tojson.setOnClickListener(newOnClickListener()
{
@Override
publicvoidonClick(View v)
{
mQueue.add(stringRequest);
}
});
}
}
其中上面的RequestQueue是开源网络库Volley的使用,如果你对该库的使用还不熟悉的话可以参考http://blog.csdn.net/guolin_blog/article/details/17482095,该作者对Volley库的使用讲解得非常的细致和深入
大家可以仔细的去拜读。