gpt4 book ai didi

java - Side JsonObject 中的 Android 可解析 JsonArray

转载 作者:行者123 更新时间:2023-12-01 18:10:55 26 4
gpt4 key购买 nike

当我尝试使用 GSON 解析此 json 数据时。我无法在 JsonObject 中获取 JsonArray。以下是我的代码,每个建议都会得到采纳。

来自服务器的 JSON 数据:

{
"GetJobDetails": {
"MaxAmount": 0,
"CreatorId": 1,
"JobImages": [
{
"ImagePath": "http://192.168.1.108:8088/Uploads/6e660c0c-4a2b- 42dc-ad97-82cc3efe87a0.jpg",
"JobImageId": 1
},
{
"ImagePath": "http://192.168.1.108:8088/Uploads/ccf1087d-9f7e-4c21-bc61-8aa3fd924e05.jpg",
"JobImageId": 2
},
{
"ImagePath": "http://192.168.1.108:8088/Uploads/4333e8b6-0079-457f-a225-fd7900ea81b1.jpg",
"JobImageId": 3
}
],
}
}

Activity 中:

    Gson gson = new Gson();
String response = new String(mresponce);
JobDetails jobDetails= gson.fromJson(response, JobDetails .class);

Log.e("JobDetails ",""+jobDetails.getJobImagesList());

即使我的数据中有图像列表,此日志也始终打印为空。

型号类别:

public class JobDetails implements Parcelable {

private int MaxAmount;
private int CreatorId;
private List<JobImage> JobImages;

public JobDetails() {

}

public JobDetails(Parcel parcel) {
this.MaxAmount = parcel.readInt();
this.CreatorId= parcel.readInt();
this.JobImages = new ArrayList<JobImage>();
parcel.readTypedList(JobImages, JobImage.CREATOR);

}

// Parcelable
@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.MaxAmount);
dest.writeInt(this.CreatorId);
dest.writeList(this.JobImages);
// TODO: Not Parceling AddressList
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public JobDetails createFromParcel(Parcel in) {
return new JobDetails(in);
}
public JobDetails[] newArray(int size) {
return new JobDetails[size];
}
};

public List<JobImage> getJobImagesList() {
return JobImages;
}

public void setJobImagesList(List<JobImage> jobImages) {
JobImages = jobImages;
}
public int getMaxAmount() {
return MaxAmount;
}

public void setMaxAmount(int maxAmount) {
MaxAmount= maxAmount;
}
}

JOBIMAGE 的另一个模型类:

public class JobImage implements Parcelable {

private String ImagePath;
private int JobImageId;
JobImage(){

}
public JobImage(Parcel in) {
this.ImagePath = in.readString();
this.JobImageId = in.readInt();
}
// Parcelable
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.ImagePath);
dest.writeInt(this.JobImageId);
// TODO: Not Parceling AddressList
}
public static final Creator CREATOR = new Creator() {
public JobImage createFromParcel(Parcel in) {
return new JobImage(in);
}
public JobImage[] newArray(int size) {
return new JobImage[size];
}
};

public String getImagePath() {
return ImagePath;
}

public void setImagePath(String imagePath) {
ImagePath = imagePath;
}

public int getJobImageId() {
return JobImageId;
}

public void setJobImageId(int jobImageId) {
JobImageId = jobImageId;
}
}

请帮我找出我做错了什么:

最佳答案

您的顶级 JSON 对象不是 JobDetails 对象,它是一个具有 JobDetails 成员名称 GetJobDetails 的对象。您需要处理此级别的 JSON。您可以使用自定义 TypeAdapter 来完成此操作,或者可能更简单,只需创建一个容器对象并将其反序列化即可。

class JobDetailContainer {
private JobDetails GetJobDetails;
public JobDetails getJobDetails() {
return GetJobDetails;
}
}

然后使用--

Gson gson = new Gson();
String response = new String(mresponce);
GetJobDetails getJobDetails= gson.fromJson(response, GetJobDetails.class);

Log.e("JobDetails ",""+getJobDetails.getJobDetails().getJobImagesList());

关于java - Side JsonObject 中的 Android 可解析 JsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33080710/

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