在进行json数据解析的时候,发生了如下的报错:
报错内容:
2022-04-12 19:20:38.381 5140-5140/com.example.jsonexestud E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jsonexestud, PID: 5140
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 68 path $[2]
at com.google.gson.Gson.fromJson(Gson.java:947)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.example.jsonexestud.MainActivity.getInfosFromJson(MainActivity.java:83)
at com.example.jsonexestud.MainActivity$1.handleMessage(MainActivity.java:56)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6198)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 68 path $[2]
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1564)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:477)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:415)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:897)?
at com.google.gson.Gson.fromJson(Gson.java:846)?
at com.example.jsonexestud.MainActivity.getInfosFromJson(MainActivity.java:83)?
at com.example.jsonexestud.MainActivity$1.handleMessage(MainActivity.java:56)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:154)?
at android.app.ActivityThread.main(ActivityThread.java:6198)?
at java.lang.reflect.Method.invoke(Native Method)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)?
2022-04-12 19:20:38.895 1872-1872/? E/EGL_emulation: tid 1872: eglCreateSyncKHR(1585): error 0x3004 (EGL_BAD_ATTRIBUTE)
?程序的主要代码:
final Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//获取子线程传递过来的json数据并保存到变量中
String json= (String) msg.obj;
List<WeatherInfo> weatherInfos = null;
//调用getInfosFromJson()方法,将天气信息集合保存到weatherInfos中
try{
weatherInfos=getInfosFromJson(json);
}catch (IOException e) {
e.printStackTrace();
}
//循环读取weatherInfos中的每一条数据
list = new ArrayList<Map<String, String>>();
for (WeatherInfo info : weatherInfos) {
map = new HashMap<String, String>();
map.put("temp", info.getTemp());
map.put("weather", info.getWeather());
map.put("name", info.getName());
map.put("pm", info.getPm());
map.put("wind", info.getWind());
list.add(map);
}
}
};
//解析json数据返回天气信息的集合
public static List<WeatherInfo> getInfosFromJson(String json)
throws IOException { ;
//使用gson库解析JSON数据
Gson gson=new Gson();
Type listType=new TypeToken<List<WeatherInfo>>(){}.getType();
List<WeatherInfo> list=gson.fromJson(json,listType);
return list;
}
主要的错误之处在于:
Unterminated array at line 2 column 68 path $[2]
主要是因为在json文件中有一些特殊的符号,或者是中英文的符号标点的问题
我这次就是因为json文件中有一个英文符号写成了英文的导致了,程序的运行失败;
?
|