Java 如何通过对对象中的对象进行改造来解析json数据?
我在web上看到了许多用于改造的JSON示例,但找不到我拥有的JSON结构类型。我没办法解决它。我有以下json数据,我正试图用java在我的android应用程序中显示这些数据:Java 如何通过对对象中的对象进行改造来解析json数据?,java,android,json,retrofit,Java,Android,Json,Retrofit,我在web上看到了许多用于改造的JSON示例,但找不到我拥有的JSON结构类型。我没办法解决它。我有以下json数据,我正试图用java在我的android应用程序中显示这些数据: { "main": { "data": [ { "Date": "2020-06-15", "name": "mary", &quo
{
"main": {
"data": [
{
"Date": "2020-06-15",
"name": "mary",
"address": "NY"
},
{
"Date": "2020-06-15",
"name": "bob",
"adress": "LA"
},
{
"Date": "2020-06-15",
"name": "John",
"address": "CA"
}
]
}
}
此外,我还学习了以下模型课程:
-----------------------------------com.example.Datum.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Datum {
@SerializedName("date")
@Expose
private String date;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("main")
@Expose
private Main main;
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
}
-----------------------------------com.example.Main.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Main {
@SerializedName("data")
@Expose
private List<Datum> data = null;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
}
------------------------------------com.example.Datum.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类数据{
@序列化名称(“日期”)
@暴露
私有字符串日期;
@序列化名称(“名称”)
@暴露
私有字符串名称;
@序列化名称(“地址”)
@暴露
私有字符串地址;
公共字符串getDate(){
返回日期;
}
公共无效设置日期(字符串日期){
this.date=日期;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
}
-----------------------------------com.example.example.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课范例{
@序列化名称(“主”)
@暴露
私人主楼;
公共Main getMain(){
回水总管;
}
公共无效设置主(主){
this.main=main;
}
}
-----------------------------------com.example.Main.java-----------------------------------
包com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共班机{
@SerializedName(“数据”)
@暴露
私有列表数据=null;
公共列表getData(){
返回数据;
}
公共无效设置数据(列表数据){
这个数据=数据;
}
}
然而,我无法使接口类和改型中使用的模型。请举例说明合适的方法。谢谢。我想你需要一门像这样的
课程:
public class Main{
@SerializedName("main")
private Main main;
@SerializedName("data")
private List<Datum> datas;
public void setMain(Main main){
this.main = main;
}
public Main getMain(){
return main;
}
public void setCustomers(List<Datum> datas){
this.datas = datas;
}
public List<Datum> getDatas(){
return datas;
}
}
public class Datum {
@SerializedName("date")
@Expose
private String date;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
@GET("/")
Call<Main> getMain();
您应该有如下的API和API接口
:
public class Main{
@SerializedName("main")
private Main main;
@SerializedName("data")
private List<Datum> datas;
public void setMain(Main main){
this.main = main;
}
public Main getMain(){
return main;
}
public void setCustomers(List<Datum> datas){
this.datas = datas;
}
public List<Datum> getDatas(){
return datas;
}
}
public class Datum {
@SerializedName("date")
@Expose
private String date;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
@GET("/")
Call<Main> getMain();
@GET(“/”)
调用getMain();
出现了什么错误?为什么这里需要接口类?我希望您使用jsonschema2pojo@vinv:我得到的结果为空。@Sumit Sahoo:要定义诸如“@GET(“/”)Call getMain();”之类的改装调用,请选中@milad answer。它应该可以工作。客户来自哪里?@Bishwash,将其更改为数据,我更新了我的答案。它应该是@SerializedName(“日期”)。其余都很好。@vinv,来自服务器的响应是date,请再次检查问题!!