gpt4 book ai didi

java - 改造 - java.lang.IllegalStateException : Expected BEGIN_ARRAY but was BEGIN_OBJECT

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:44 31 4
gpt4 key购买 nike

我正在尝试解析我自己的 JSON,但得到了 JSONSyntaxException,这是我的 JSON 的样子:

{
"type":"success",
"value":[
{
"id":1,
"title":"Title - 1",
"name":{
"first":"First - 1",
"last":"Last - 1"
},
"hobbies":[
"Writing Code - 1",
"Listening Music - 1"
]
},
.....
]
}

日志说:

E/app.retrofit_chucknorries.MainActivity$2: ERROR: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
at line 7 column 12 path $.value[0].name
01-21 12:41:52.156 28936-28936/app.retrofit_chucknorries
W/System.err: retrofit.RetrofitError: com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
at line 7 column 12 path $.value[0].name

我在哪里做错误?我只是根据我的要求和类做了一些小修改,其他一切几乎与原始代码相同Value.java:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class Value {

@SerializedName("id")
@Expose
private Integer id;

@SerializedName("title")
@Expose
private String title;

@SerializedName("hobbies")
@Expose
private List<String> hobbies = new ArrayList<String>();

@SerializedName("name")
@Expose
private List<Name> name = new ArrayList<Name>();

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public List<Name> getName() {
return name;
}

public void setName(List<Name> name) {
this.name = name;
}

public List<String> getHobbies() {
return hobbies;
}

public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

最佳答案

在你的 Master.java 类中,你的名字不是数组!

private List<Name> name = new ArrayList<Name>();

改成这个并尝试:

 private Name name;

实际上,通过查看异常日志您可以知道这一点。

关于java - 改造 - java.lang.IllegalStateException : Expected BEGIN_ARRAY but was BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34917713/

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