上一篇博客,Android-解析JSON数据(JSON对象/JSON数组),介绍了使用 org.json.JSONArray;/org.json.JSONObject; 来解析JSON数据;
Google Android 还提供来另外一种方式来解析JSON数据,那就是Gson;
Gson是非常方便的JSON解析/封装/处理等等,强大的工具类:
特点:Gson可以把JSON对象数据->转换映射为Bean对象
Gson可以把JSON数组数据->转换映射为集合
Gson可以把Bean对象->转换为JSON对象数据
Gson可以把集合->转换为JSON数组数据
...........
首先要在app/build.gradle配置文件中,导入,Gson支持包
// Gson支持包的导入
implementation 'com.google.code.gson:gson:2.6.2'
需要解析的JSON数据:
/data/data/liudeli.mynetwork01/files/pottingJSON1
{
"name":"李四",
"age":99,
"hobby":"爱好是练习截拳道"
}
/data/data/liudeli.mynetwork01/files/pottingJSONArray1
[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
定义一个Bean
定义的name/age/hobby 必须要和JSON数据里面的一模一样
package liudeli.mynetwork01.entity; /** * 定义一个Bean * 定义的name/age/hobby 必须要和JSON数据里面的一模一样 * { * "name":"李四", * "age":99, * "hobby":"爱好是练习截拳道" * } */ public class Student2 { private String name; private int age; private String hobby; public Student2(String name, int age, String hobby) { this.name = name; this.age = age; this.hobby = hobby; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", hobby='" + hobby + '\'' + '}'; } }
定义的name/age/sex 必须要和JSON数据里面的一模一样
package liudeli.mynetwork01.entity; /** * 定义一个Bean * 定义的name/age/sex 必须要和JSON数据里面的一模一样 * * [ * { * "name":"君君", * "age":89, * "sex":"男" * }, * { * "name":"小君", * "age":99, * "sex":"女" * }, * { * "name":"大君", * "age":88, * "sex":"男" * } * ] */ public class Student { private String name; private int age; private String sex; public Student(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
GsonAnalyzeJSONActivity.java
package liudeli.mynetwork01; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import liudeli.mynetwork01.entity.Student; import liudeli.mynetwork01.entity.Student2; public class GsonAnalyzeJSONActivity extends Activity { private final String TAG = GsonAnalyzeJSONActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gson_analyze); } /** * Gson解析JSON对象 * { * "name":"李四", * "age":99, * "hobby":"爱好是练习截拳道" * } */ public void gonsAnalyzeJSONObject(View view) { String jsonData = readFile("pottingJSON1"); // Log.d(TAG, "jsonData:" + jsonData); Gson gson = new Gson(); Student2 student2 = gson.fromJson(jsonData, Student2.class); Log.d(TAG, "gonsAnalyzeJSONObject 解析后的结果:" + student2.toString()); } /** * Gson解析JSON数组 * [ * { * "name":"君君", * "age":89, * "sex":"男" * }, * { * "name":"小君", * "age":99, * "sex":"女" * }, * { * "name":"大君", * "age":88, * "sex":"男" * } * ] * @param view */ public void gonsAnalyzeJSONArray(View view) { String jsonData = readFile("pottingJSONArray1"); // Log.d(TAG, "jsonData:" + jsonData); Gson gson = new Gson(); /** * TypeToken<List<需要映射的Bean对象>>(){}.getType() */ List<Student> list = gson.fromJson(jsonData, new TypeToken<List<Student>>(){}.getType()); // 参数二:需要指定类型,类型来决定解析的集合 for (Student student: list) { Log.d(TAG, "gonsAnalyzeJSONArray 解析后的结果:" + student.toString()); } } /** * 读取文件里面的字符串 * @param fileName * @return */ private String readFile(String fileName) { String result = null; try { InputStream inputStream = openFileInput(fileName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes); baos.write(bytes, 0,bytes.length); result = new String(baos.toByteArray()); baos.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } return result; } }
activity_gson_analyze.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gson解析JSON对象" android:onClick="gonsAnalyzeJSONObject" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gson解析JSON数组" android:onClick="gonsAnalyzeJSONArray" /> </LinearLayout>
日志的打印:
使用Gson解析,JSON对象数据:
12-23 23:00:52.108 9729-9729/liudeli.mynetwork01 D/GsonAnalyzeJSONActivity: gonsAnalyzeJSONObject 解析后的结果:Student{name='李四', age=99, hobby='爱好是练习截拳道'}
使用Gson解析,JSON数组数据:
12-23 23:00:53.199 9729-9729/liudeli.mynetwork01 D/GsonAnalyzeJSONActivity: gonsAnalyzeJSONArray 解析后的结果:Student{name='君君', age=89, sex='男'}
12-23 23:00:53.199 9729-9729/liudeli.mynetwork01 D/GsonAnalyzeJSONActivity: gonsAnalyzeJSONArray 解析后的结果:Student{name='小君', age=99, sex='女'}
12-23 23:00:53.199 9729-9729/liudeli.mynetwork01 D/GsonAnalyzeJSONActivity: gonsAnalyzeJSONArray 解析后的结果:Student{name='大君', age=88, sex='男'}