本文将带您了解关于JavaGSON抛出“预期是BEGIN_OBJECT,但是是BEGIN_ARRAY”?的新内容,同时我们还将为您解释java抛出danger的相关知识,另外,我们还将为您提供关于An
本文将带您了解关于Java GSON抛出“预期是BEGIN_OBJECT,但是是BEGIN_ARRAY”?的新内容,同时我们还将为您解释java抛出danger的相关知识,另外,我们还将为您提供关于Android JSon错误“预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY”、Android JSon错误“预计BEGIN_OBJECT但在第1行第2列是BEGIN_ARRAY”、android – Moshi Retrofit错误:“预期字符串但是BEGIN_OBJECT”、android-预期为BEGIN_ARRAY,但在第1行第35列为BEGIN_OBJECT的实用信息。
本文目录一览:- Java GSON抛出“预期是BEGIN_OBJECT,但是是BEGIN_ARRAY”?(java抛出danger)
- Android JSon错误“预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY”
- Android JSon错误“预计BEGIN_OBJECT但在第1行第2列是BEGIN_ARRAY”
- android – Moshi Retrofit错误:“预期字符串但是BEGIN_OBJECT”
- android-预期为BEGIN_ARRAY,但在第1行第35列为BEGIN_OBJECT
Java GSON抛出“预期是BEGIN_OBJECT,但是是BEGIN_ARRAY”?(java抛出danger)
我正在尝试解析这样的JSON字符串
[ { "updated_at":"2012-03-02 21:06:01", "fetched_at":"2012-03-02 21:28:37.728840", "description":null, "language":null, "title":"JOHN", "url":"http://rus.JOHN.JOHN/rss.php", "icon_url":null, "logo_url":null, "id":"4f4791da203d0c2d76000035", "modified":"2012-03-02 23:28:58.840076" }, { "updated_at":"2012-03-02 14:07:44", "fetched_at":"2012-03-02 21:28:37.033108", "description":null, "language":null, "title":"PETER", "url":"http://PETER.PETER.lv/rss.php", "icon_url":null, "logo_url":null, "id":"4f476f61203d0c2d89000253", "modified":"2012-03-02 23:28:57.928001" }]
进入对象列表。
List<ChannelSearchEnum> lcs = (List<ChannelSearchEnum>) new Gson().fromJson( jstring , ChannelSearchEnum.class);
这是我正在使用的对象类。
import com.google.gson.annotations.SerializedName;public class ChannelSearchEnum {@SerializedName("updated_at")private String updated_at;@SerializedName("fetched_at")private String fetched_at;@SerializedName("description")private String description;@SerializedName("language")private String language;@SerializedName("title")private String title;@SerializedName("url")private String url;@SerializedName("icon_url")private String icon_url;@SerializedName("logo_url")private String logo_url;@SerializedName("id")private String id;@SerializedName("modified")private String modified;public final String get_Updated_at() { return this.updated_at;}public final String get_Fetched_at() { return this.fetched_at;}public final String get_Description() { return this.description;}public final String get_Language() { return this.language;}public final String get_Title() { return this.title;}public final String get_Url() { return this.url;}public final String get_Icon_url() { return this.icon_url;}public final String get_Logo_url() { return this.logo_url;}public final String get_Id() { return this.id;}public final String get_Modified() { return this.modified;} }
但这让我着迷
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
有什么想法我应该如何解决?
答案1
小编典典问题是你要告诉Gson
你具有你类型的对象。你不知道 你有一系列类型的对象。你不能只是尝试像这样投射结果并期望它神奇地工作;)
《用户指南》Gson
介绍了如何处理此问题:
https://github.com/google/gson/blob/master/UserGuide.md
这将起作用:
ChannelSearchEnum[] enums = gson.fromJson(yourJson, ChannelSearchEnum[].class);
但这更好:
Type collectionType = new TypeToken<Collection<ChannelSearchEnum>>(){}.getType();Collection<ChannelSearchEnum> enums = gson.fromJson(yourJson, collectionType);
Android JSon错误“预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY”
我从Web服务获取JSon数据,示例数据如下:
[ { "SectionId": 1, "SectionName": "Android" }]
当我尝试将其转换时,它将引发错误,我这样做是:
Data data = new Gson().fromJson(jsonDataFromWebService, Data.class);
我的班级是:
class Section{ public int SectionId; public String SectionName;}class Data { public List<Section> sections;}
LogCat说:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY
答案1
小编典典错误说明发生了什么问题…您返回的是数组而不是JSon对象
尝试如下:
JSONArray ja = new JSONArray(jsonStringReturnedByService);Data sections = new Data();for (int i = 0; i < ja.length(); i++) { Section s = new Section(); JSONObject jsonSection = ja.getJSONObject(i); s.SectionId = Integer.ValueOf(jsonSection.getString("SectionId")); s.SectionName = jsonSection.getString("SectionName"); //add it to sections list sections.add(s);}return sections;
Android JSon错误“预计BEGIN_OBJECT但在第1行第2列是BEGIN_ARRAY”
[ { "SectionId": 1,"SectionName": "Android" } ]
当我尝试转换它时,它会抛出一个错误,我这样做:
Data data = new Gson().fromJson(jsonDataFromWebService,Data.class);
我的科级是:
class Section { public int SectionId; public String SectionName; } class Data { public List<Section> sections; }
LogCat说:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
解决方法
尝试如下:
JSONArray ja = new JSONArray(jsonStringReturnedByService); Data sections = new Data(); for (int i = 0; i < ja.length(); i++) { Section s = new Section(); JSONObject jsonSection = ja.getJSONObject(i); s.SectionId = Integer.ValueOf(jsonSection.getString("SectionId")); s.SectionName = jsonSection.getString("SectionName"); //add it to sections list sections.add(s); } return sections;
android – Moshi Retrofit错误:“预期字符串但是BEGIN_OBJECT”
我像这样构建了我的Retrofit实例:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(moshiConverterFactory.create(moshi))
.build();
然后我这样调用我的MockWebServer实例:
server.enqueue(new MockResponse().setBody(jsonStr));
jsonStr的构建方式如下:
MyModel model = new MyModel("HOME", "AWAY", "ENTERTAIN", "NIGHT", "MUTE",
"VOLUME", "SCENE 1", "SCENE 2", "SCENE 3");
JsonAdapter<MyModel> jsonAdapter = moshi.adapter(MyModel.class).toJson(model);
但是,代码崩溃了:
Response response = api.getString().execute();
例外是:
com.squareup.moshi.JsonDataException: Expected a string but was BEGIN_OBJECT at path $
我做错了什么?
解决方法:
我找到了解决方案:
我的api界面需要
@GET(“/”)调用< JsonObject>的GetString();
不
@GET(“/”)调用< String>的GetString();
原因是我在模拟JSON响应,而不是普通的String.
android-预期为BEGIN_ARRAY,但在第1行第35列为BEGIN_OBJECT
我刚从gson开始,我想解析一个以对象开头的JSON字符串,总是得到相同的错误
JSON格式
{
"code": 200,
"data": {
"messages": [
{
"emailSender": "dsfd@mail.ru",
"countryCode": null,
"emailSenderReply": null,
"rejectedReason": null,
"messageReplySenderMessageDeleted": null,
"lastNameReceiver": null,
"wpMessagesratingReplyId": null,
"wpMessagesratingrating": null,
"countryMemberId": 143,
"phonesenderReply": null,
"messageReplyReceiverMessageDeleted": null,
"readStatus": "unread",
"phoneReceiverReply": null,
"membeRSSenderUid": "m8692031",
"wpMessagesRequestTitle": "Fazzzzzz",
"title": "Fazzzzzz",
"countryTitle": null,
"emailReceiver": null,
"firstNameReceiverReply": null,
"id": 1288,
"messageReplyId": null,
"membersReceiverUid": "m1000002",
"time": "2014-12-28 14:32:09",
"wpMessagesRequestCategoryId": 4,
"lastNameReceiverReply": null,
"lastNameSender": "dsad",
"phoneReceiver": null,
"status": "unanswered",
"messageReplyReceiver": null,
"messageReplyStatus": null,
"memberReceiverRole": "admin",
"isConsultant": 1,
"roleReplyReceiver": null,
"wpMesssagesRequestCategoriesSystemName": "orders",
"lastNameSenderReply": null,
"memberSenderRole": "member",
"wpMesssagesRequestCategoriesName": "Orders",
"requestMessage": 1101,
"wpMessagesRequestPriority": "middle",
"messageReplyTime": null,
"message": "OLOLO",
"wpMessagesRequestCountryId": null,
"sender": 4481,
"firstNameReceiver": null,
"messageReplyMessage": null,
"firstNameSender": "asdas",
"firstNameSenderReply": null,
"emailReceiverReply": null,
"roleReplySender": null,
"messageReplySender": null,
"wpMessagesRequestProductId": null,
"receiver": 4364,
"isMessageForwarded": 0,
"wpMessagesRequestStatus": "not-taken",
"phonesender": "2(342)-4-23-42",
"wpMessagesRequestMessage": "OLOLO"
}
]
}
}
我一直遇到错误:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 3 column 12
我的日志中的错误指向这一行:
Gson gson = new GsonBuilder().create();
Main main=gson.fromJson(a, Main.class);
这是我的pojo
static class Main{
int code;
boolean error;
List<Data> data;
}
static class Data{
Enteties messages;
}
static class Enteties{
String msg_title;
String msg_time;
int msg_id;
String msg_status;
@Override
public String toString(){
return msg_title+" "+msg_time+" "+msg_id+" "+msg_status;
}
}
解决方法:
您的主类将数据作为列表.您的JSON将其作为对象.类型需要匹配.如果您希望main中只有1个数据,请不要使用列表.如果期望1个或多个数据,则使生成数据的代码向下发送一个数组(即使该数组中只有1个对象).
关于Java GSON抛出“预期是BEGIN_OBJECT,但是是BEGIN_ARRAY”?和java抛出danger的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android JSon错误“预期为BEGIN_OBJECT,但在第1行第2列为BEGIN_ARRAY”、Android JSon错误“预计BEGIN_OBJECT但在第1行第2列是BEGIN_ARRAY”、android – Moshi Retrofit错误:“预期字符串但是BEGIN_OBJECT”、android-预期为BEGIN_ARRAY,但在第1行第35列为BEGIN_OBJECT的相关知识,请在本站寻找。
本文标签: