·在安卓中,可以使用第三方的Gson来解析json文件。
·以下是解析保存了天气信息的json文件过程:
1、向当前项目中引入Gson的第三方包。引入方法:https://blog.csdn.net/qq_36418170/article/details/81430095(感谢这位博主)
2、准备好一个保存了天气信息的json文件:weather2.json
[
{"city":"bj","temp":"23","wind":"南风3级","weather":"晴天"},
{"city":"sh","temp":"12","wind":"南风234级","weather":"阴天"}
]
3、编写解析json的工具类WeatherService.java,提供一个静态方法
//解析json文件,返回天气信息
public static List<WeatherInfo> getWeatherInfoFromJson(InputStream is) throws Exception {
byte[] bytes = new byte[is.available()];
is.read(bytes);//将输入流读取到字节数组中
String json = new String(bytes,"utf-8");//通过字节数组生成字符串
//使用Gson
Gson gson = new Gson();
Type listType = new TypeToken<List<WeatherInfo>>(){}.getType(); //获得Json转换目标类型
List<WeatherInfo> list = gson.fromJson(json, listType);
return list;
}
4、编写一个简单的界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/temp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/wind"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:id="@+id/weather"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:id="@+id/bt_bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bj"
/>
<Button
android:id="@+id/bt_sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sh"
/>
</LinearLayout>
5、在activity中调用工具类,对界面中的TextView的text进行赋值
package cn.edu.sjzc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_city;
private TextView tv_temp;
private TextView tv_wind;
private TextView tv_weather;
List<Map<String,String>> list;
private Map<String,String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//解析Json
InputStream inputStream = getResources().openRawResource(R.raw.weather2);//得到Json文件
try {
List<WeatherInfo> weatherInfos = WeatherService.getWeatherInfoFromJson(inputStream);
list = new ArrayList<>();
for (WeatherInfo weatherInfo:weatherInfos) {
map = new HashMap<>();
map.put("city",weatherInfo.getCity());
map.put("temp",weatherInfo.getTemp());
map.put("wind",weatherInfo.getWind());
map.put("weather",weatherInfo.getWeather());
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
//自定义方法,讲天气信息传到Textview中
getMap(0);
}
private void getMap(int number) {
Map<String,String> oneMap = list.get(number);//取出一个mapper
String city = oneMap.get("city");
String temp = oneMap.get("temp");
String wind = oneMap.get("wind");
String weather = oneMap.get("weather");
tv_city.setText(city);
tv_temp.setText(temp);
tv_wind.setText(wind);
tv_weather.setText(weather);
}
private void initView() {
tv_city= findViewById(R.id.city);
tv_temp = findViewById(R.id.temp);
tv_wind = findViewById(R.id.wind);
tv_weather = findViewById(R.id.weather);
findViewById(R.id.bt_bj).setOnClickListener(this);
findViewById(R.id.bt_sh).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_bj://说明点击的是北京
getMap(0);
break;
case R.id.bt_sh:
getMap(1);
break;
}
}
}
6、运行查看效果: