json数据如何解析

本人属于菜鸟,如果地方说错还请指点指点

下面这个段数据是从心知天气上面获取的,https://www.seniverse.com/ 走起这个就是

{
"results": [{
"location": {
"id": "WX4FBXXFKE4F",
"name": "北京",
"country": "CN",
"path": "北京,北京,中国",
"timezone": "Asia/Shanghai",
"timezone_offset": "+08:00"
},
"now": {
"text": "多云",
"code": "4",
"temperature": "16"
},
"last_update": "2018-04-12T08:30:00+08:00"
}]

}


比如上面这一段返回的json数据应该怎么写一个Bean 来获取??

整理一下应该是一个result 数组集合里头有3个子集  location、 now、 last_update

首先在json数据里头看见 [ ]  这个符号直接在类里头定义一个 List<>   ,那么我们的案例就是  list<result>

"results": [{
"location": {
},
"now": {
},
"last_update": "2018-04-12T08:30:00+08:00"
}]

然后,result 里面包含了2个对象 location{}、 now{}、一个字符串 last_update,那么每一个对象又是一个类了 

定义一个类来加载数据

public class NowWeather {
    private String text;
    private String code;
    private String temperature;}

省略了get和set,

返回的数据字段跟你的命名要一致

在Activity中,

如果是数组类型的就get(index),其他的就get他们的名字可以了


直接来个源码吧

class

package com.example.cnb.testretrofit;

import java.util.List;

/**
 * Created by cnb on 2018/4/11.
 */

public class WeatherRespone {
    private List<WeatherData> results;

    public List<WeatherData> getResults() {
        return results;
    }

    public void setResults(List<WeatherData> results) {
        this.results = results;
    }


    public class WeatherData {
        private LocationMsg location;
        private NowWeather now;
        private String last_update;

        public LocationMsg getLocation() {
            return location;
        }

        public void setLocation(LocationMsg location) {
            this.location = location;
        }

        public LocationMsg getLocationMsg() {
            return location;
        }

        public void setLocationMsg(LocationMsg locationMsg) {
            this.location = locationMsg;
        }

        public NowWeather getNow() {
            return now;
        }

        public void setNow(NowWeather now) {
            this.now = now;
        }

        public String getLast_update() {
            return last_update;
        }

        public void setLast_update(String last_update) {
            this.last_update = last_update;
        }

        public class LocationMsg {

            private String id;
            private String name;
            private String country;
            private String path;
            private String timezone;
            private String timezone_offset;

            public String getPath() {
                return path;
            }

            public void setPath(String path) {
                this.path = path;
            }


            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getCountry() {
                return country;
            }

            public void setCountry(String country) {
                this.country = country;
            }

            public String getTimezone() {
                return timezone;
            }

            public void setTimezone(String timezone) {
                this.timezone = timezone;
            }

            public String getTimezone_offset() {
                return timezone_offset;
            }

            public void setTimezone_offset(String timezone_offset) {
                this.timezone_offset = timezone_offset;
            }
        }

        
        public class NowWeather {
            private String text;
            private String code;
            private String temperature;


            public String getText() {
                return text;
            }

            public void setText(String text) {
                this.text = text;
            }

            public String getCode() {
                return code;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getTemperature() {
                return temperature;
            }

            public void setTemperature(String temperature) {
                this.temperature = temperature;
            }


        }
    }
}

activity

本人使用的是心知天气API  里面的key就申请一下账号就可以有了

package com.example.cnb.testretrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //步骤4:创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.seniverse.com/v3" + "/") // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // 步骤5:创建 网络请求接口 的实例

        IuserRespone iuserRespone = retrofit.create(IuserRespone.class);
       
         //对 发送请求 进行封装
        Call<WeatherRespone> call = iuserRespone.getUser("你自己申请的key","beijing","zh-Hans","c");

        call.enqueue(new Callback<WeatherRespone>() {
            @Override
            public void onResponse(Call<WeatherRespone> call, Response<WeatherRespone> response) {
                    String a= response.body().getResults().get(0).getLocation().getCountry();
                    Log.e("aaa", a+"   "  );
            }

            @Override
            public void onFailure(Call<WeatherRespone> call, Throwable t) {

            }
        });


    }

    public interface IuserRespone {
        @GET("weather/now.json")
        Call<WeatherRespone> getUser(@Query("key") String key,@Query("location") String location,
                                  @Query("language") String language,@Query("unit") String unit);
        // 注解里传入 网络请求 的部分URL地址
        // Retrofit把网络请求的URL分成了两部分:一部分放在Retrofit对象里,另一部分放在网络请求接口里
        // 如果接口里的url是一个完整的网址,那么放在Retrofit对象里的URL可以忽略
        // getCall()是接受网络请求数据的方法
    }
}






猜你喜欢

转载自blog.csdn.net/qq_36099573/article/details/79907442