一、GsonFormat / Gson 共同解析 json 文件
首先在AS的setting->plugin 中 install GsonFormat 插件,然后在app.gradle 文件中导入 Gson 解析类,
implementation 'com.google.code.gson:gson:2.8.5'
MainActivity.java 文件如下:
public class MainActivity extends Activity {
String weatherStr = "{ \n" +
" \"username\" : \"mary\", \n" +
" \"age\":\"20\", \n" +
" \"info\":{\"tel\":\"1234566\",\"celltelphone\":788666}, \n" +
" \"address\":[ \n" +
" {\"city\":\"beijing\",\"code\":\"1000022\"}, \n" +
" {\"city\":\"shanghai\",\"code\":\"2210444\"} \n" +
" ] \n" +
" }";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
Weather weather = gson.fromJson(weatherStr,Weather.class);
String tel = weather.getInfo().getTel();
String city = weather.getAddress().get(1).getCity();
Log.e("weather = ","tel =" + tel + "city =" + city);
}
}
08-27 02:08:53.683 10840-10840/com.nxyuntui.mapyuntui E/weather =: tel =1234566city =shanghai