“预期为 BEGIN_ARRAY,但在路径 $ 处为 STRING
Posted
技术标签:
【中文标题】“预期为 BEGIN_ARRAY,但在路径 $ 处为 STRING【英文标题】:“Expected BEGIN_ARRAY but was STRING at path $ 【发布时间】:2019-06-16 14:53:41 【问题描述】:我有这个方法。
Moshi moshi = new Moshi.Builder().build();
Type listMyData = Types.newParameterizedType(List.class, Feed.class);
JsonAdapter<List<Feed>> adapter = moshi.adapter(listMyData);
List<Feed> feeds=adapter.fromJson(response);
EditText txtFeeds = getActivity().findViewById(R.id.Feeds);
txtFeeds.setText(feeds.get(0).getFeeds_Header());
我的 JSON 字符串是
`[\"id\":\"877CB447-DC39-4AAD-8B40-A85F867C595A\",\"Feeds_Header\":\"Tentative Inter-se Seniority List of Tech. Asstt.\\\/ Progress Asstt.\\\/ Inspector (Statistics)\",\"Feeds_Date\":\"2019-01-16T00:00:00\",\"id\":\"F69213B9-A22D-4B0D-ABA4-14C3F5755766\",\"Feeds_Header\":\"Recruitment of 1 post of Vigyan Mandir Officer (VMO) on transfer from amongst Lecturers of Science\",\"Feeds_Date\":\"2019-01-11T00:00:00\"]`
JSON 字符串是从 WebAPI 获取的,我在 Gson、Jackson 和 Moshi 中尝试过,但无法解析。
但是当我声明一个字符串变量并使用 json 字符串对其进行初始化时,它可以正常工作。
String json = "[\"id\":\"877CB447-DC39-4AAD-8B40-A85F867C595A\",\"Feeds_Header\" ...
这是我的 Feed 类
public class Feed
@SerializedName("id")
@Expose
private String id;
public String getId()
return id;
public void setId(String id)
this.id = id;
@SerializedName("Feeds_Header")
@Expose
private String Feeds_Header;
public String getFeeds_Header()
return Feeds_Header;
public void setFeeds_Header(String feedsHeader)
this.Feeds_Header = feedsHeader;
@SerializedName("Feeds_Date")
@Expose
private String Feeds_Date;
public String getFeeds_Date()
return Feeds_Date;
public void setFeeds_Date(String feedsDate)
this.Feeds_Date = feedsDate;
【问题讨论】:
因为您得到的 json 包含一些无效字符,例如“\”,这使其成为错误的 json,请在编辑器中检查 Json 结构。 我检查了jsonlint.com的json,上面写着“有效的json” 它不是,它显示有效 json 的唯一原因是因为你在开始和结束时包含 " this 使它成为一个字符串,一个 json 数组以 [ 开头,一个 json 对象以 开头。删除开头和结尾的反逗号,它将显示无效 json 的原因 我也想到了,试了这段代码 List在进行解析时,您正在解析一个实际上是一个数组的字符串。 所以确认响应,然后相应地制作你的 POJO 类
【讨论】:
以上是关于“预期为 BEGIN_ARRAY,但在路径 $ 处为 STRING的主要内容,如果未能解决你的问题,请参考以下文章