下面是“Android利用Gson解析嵌套多层的Json的简单方法”的完整攻略。
- 导入Gson库
首先需要在项目的build.gradle
文件中添加Gson库的依赖:
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
- 创建Java类
假设我们有以下json数据:
{
"name": "张三",
"age": 18,
"gender": "male",
"address": {
"province": "广东省",
"city": "深圳市",
"district": "宝安区",
"street": "西乡街道"
},
"phone_numbers": ["13888888888", "13999999999"]
}
我们需要定义Java类来保存这些数据。示例代码如下:
import java.util.List;
public class Person {
private String name;
private int age;
private String gender;
private Address address;
private List<String> phone_numbers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<String> getPhoneNumbers() {
return phone_numbers;
}
public void setPhoneNumbers(List<String> phoneNumbers) {
this.phone_numbers = phoneNumbers;
}
public static class Address {
private String province;
private String city;
private String district;
private String street;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
}
这里需要注意的是,Address
类是定义在Person
类内部的静态类。
- 使用Gson解析Json
Gson提供了解析Json数据的方法。在本例中,我们只需要使用如下代码即可解析:
Gson gson = new Gson();
Person person = gson.fromJson(jsonString, Person.class);
其中,jsonString
是包含Json数据的字符串。解析结果是一个Person
对象。
- 提取数据
Json数据中的数据可以通过Person
对象的getter方法获取。示例如下:
String name = person.getName();
int age = person.getAge();
String gender = person.getGender();
Person.Address address = person.getAddress();
String province = address.getProvince();
String city = address.getCity();
String district = address.getDistrict();
String street = address.getStreet();
List<String> phoneNumbers = person.getPhoneNumbers();
以上代码将从Person
对象中提取所有的数据。
示例1:解析嵌套多层的Json
假如我们有以下json数据:
{
"name": "小明",
"age": 18,
"gender": "male",
"address": {
"province": "北京市",
"city": "北京市",
"district": "海淀区",
"street": "中关村"
},
"hobbies": [
{
"name": "羽毛球",
"level": "高手"
},
{
"name": "游泳",
"level": "菜鸟"
}
]
}
我们需要定义Java类来保存这些数据。示例代码如下:
public class Person {
private String name;
private int age;
private String gender;
private Address address;
private List<Hobby> hobbies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(List<Hobby> hobbies) {
this.hobbies = hobbies;
}
public static class Address {
private String province;
private String city;
private String district;
private String street;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
public static class Hobby {
private String name;
private String level;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
}
我们可以使用以下代码解析Json:
Gson gson = new Gson();
Person person = gson.fromJson(jsonString, Person.class);
数据提取和上一个示例类似,这里不再赘述。
示例2:解析包含嵌套数组的Json
假设我们有以下json数据:
{
"pages": [
{
"title": "主页",
"url": "http://www.example.com/",
"subpages": [
{
"title": "新闻",
"url": "http://www.example.com/news"
},
{
"title": "产品",
"url": "http://www.example.com/products",
"subpages": [
{
"title": "手机",
"url": "http://www.example.com/products/mobile-phones"
},
{
"title": "电视",
"url": "http://www.example.com/products/tv"
}
]
},
{
"title": "关于我们",
"url": "http://www.example.com/about-us"
}
]
}
]
}
我们需要定义Java类来保存这些数据。示例代码如下:
import java.util.List;
public class Website {
private List<Page> pages;
public List<Page> getPages() {
return pages;
}
public void setPages(List<Page> pages) {
this.pages = pages;
}
public static class Page {
private String title;
private String url;
private List<Page> subpages;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<Page> getSubpages() {
return subpages;
}
public void setSubpages(List<Page> subpages) {
this.subpages = subpages;
}
}
}
这里我们使用了递归的方式定义了Page
类。我们可以使用以下代码解析Json:
Gson gson = new Gson();
Website website = gson.fromJson(jsonString, Website.class);
假设我们要打印所有子页面的标题,我们可以使用以下代码:
List<Website.Page> pages = website.getPages();
for (Website.Page page : pages) {
printTitles(page);
}
private void printTitles(Website.Page page) {
System.out.println(page.getTitle());
if (page.getSubpages() != null) {
for (Website.Page subpage : page.getSubpages()) {
printTitles(subpage);
}
}
}
以上代码会打印出所有页面的标题,包括子页面的标题。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android利用Gson解析嵌套多层的Json的简单方法 - Python技术站