gpt4 book ai didi

java - 如何使用 Retrofit2 将 json 解析为具有子类的类

转载 作者:太空狗 更新时间:2023-10-29 16:11:51 24 4
gpt4 key购买 nike

如何转换给定的json响应

{
"name" : "John",
"surname" : "Doe",
"location" : {
"name" : "Paris",
"desc" : "Welcome to Paris"
}
}

进入

class Person
{
String name;
String surname;
Location location; // new Location(String name, String desc)
}

都是关于 Person 类中的嵌套 Location

最佳答案

使用@Expose@SerializedName 注释,如

class Person
{
@SerializedName("name")
String name;

@SerializedName("surname")
String surname;

@SerializedName("location")
Location location; // new Location(String name, String desc)
}

Location

 class Location
{
@SerializedName("name")
String name;

@SerializedName("desc")
String desc;
}

添加访问数据的getter和setter方法

关于java - 如何使用 Retrofit2 将 json 解析为具有子类的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44968681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com