你好,我有这样的Json
回应
[
{
"question": "hhhhh",
"question_answer": "hhhh ",
"question_type": "question type",
"questioner_age": "questioner age",
"questioner_city": "questioner city",
"questioner_country": "questioner country",
"questioner_name": "questioner name",
"questioner_sex": "questioner sex",
"comments_allowed": "1",
"question_id": "63",
"question_date": "05/08/2017 - 19:33",
"is_public": "1"
},
{
"question": "hhhh !!",
"question_answer": "hhhh",
"question_type": [],
"questioner_age": [],
"questioner_city": [],
"questioner_country": [],
"questioner_name": "hhhhh",
"questioner_sex": [],
"comments_allowed": "1",
"question_id": "57",
"question_date": "04/30/2017 - 14:24",
"is_public": "1"
}
]
如果列为null将返回如此“question_type”的数组:[],如果不是将返回为字符串!
所以我试图在改造时得到这个回应,但我失败了,总是得到这个错误
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第4行第2行路径为$ BEGIN_ARRAY $
我在互联网上搜索后尝试了类似的东西,但它没有工作!
Gson gson = new Gson();
String json = response.body().toString();
if (json instanceof String)
{
MyQuestionModelString parseObject = gson.fromJson(json, MyQuestionModelString.class);
apiCallResponse.onSuccess(parseObject,responseMessage);
}else {
MyQuestionModel parseObject = gson.fromJson(json, MyQuestionModel.class);
apiCallResponse.onSuccess(parseObject,responseMessage);
}
任何帮助!
UPDATE!
这是我的响应模型和同样的错误!
public class MyQuestionModel {
@SerializedName("question")
@Expose
private String question;
@SerializedName("question_answer")
@Expose
private String questionAnswer;
@SerializedName("question_type")
@Expose
private List<Object> questionType = null;
@SerializedName("questioner_age")
@Expose
private List<Object> questionerAge = null;
@SerializedName("questioner_city")
@Expose
private List<Object> questionerCity = null;
@SerializedName("questioner_country")
@Expose
private List<Object> questionerCountry = null;
@SerializedName("questioner_name")
@Expose
private String questionerName;
@SerializedName("questioner_sex")
@Expose
private List<Object> questionerSex = null;
@SerializedName("comments_allowed")
@Expose
private String commentsAllowed;
@SerializedName("question_id")
@Expose
private String questionId;
@SerializedName("question_date")
@Expose
private String questionDate;
@SerializedName("is_public")
@Expose
private String isPublic;
}
我的主要问题是如何定义这个领域! question_type screen shot
在解析json期间,如果找不到SerializedName
密钥,它将抛出异常。使用@Expose
让反序列化器知道该字段可以为null。这是您提到的响应的类似模型
public class ResponsePojo {
List<Data> data;
public class Data {
@Expose
@SerializedName("question")
String question;
@Expose
@SerializedName("question_answer")
String questionAnswer;
@Expose
@SerializedName("question_type")
String questionType;
@Expose
@SerializedName("questioner_age")
String questionerAge;
@Expose
@SerializedName("questioner_city")
String questionerCity;
@Expose
@SerializedName("questioner_country")
String questionerCountry;
@Expose
@SerializedName("questioner_name")
String questionerName;
@Expose
@SerializedName("questioner_sex")
String questionerSex;
@Expose
@SerializedName("comments_allowed")
String commentsAllowed;
@Expose
@SerializedName("question_id")
String questionId;
@Expose
@SerializedName("question_date")
String questionDate;
@Expose
@SerializedName("is_public")
String isPublic;
}
}
您必须使用@Exposed标记指示模型中的哪些参数或对象是可选的。
@Expose
@SerializedName("question_type")
private String mQuestionType;
你的问题是
ava.lang.IllegalStateException:预期BEGIN_OBJECT但在第4行第2行路径$ BEGIN_ARRAY $
[...]
,你的回报是JSONArray
,你可以使用Gson
to解析它到List<Object>
。{...}
,你的回报是JSONObject
,你可以使用Gson
to解析它到Object
。所以你应该使用List<MyQuestionModel>
来获取解析数据。
在你的通话代码中将MyQuestionModel
更改为List<MyQuestionModel>
。
样品
Call<List<MyQuestionModel>> getData();
JSONEntity为你json
public class JSONEntity {
/**
* question : hhhhh
* question_answer : hhhh
* question_type : question type
* questioner_age : questioner age
* questioner_city : questioner city
* questioner_country : questioner country
* questioner_name : questioner name
* questioner_sex : questioner sex
* comments_allowed : 1
* question_id : 63
* question_date : 05/08/2017 - 19:33
* is_public : 1
*/
private String question;
private String question_answer;
private String question_type;
private String questioner_age;
private String questioner_city;
private String questioner_country;
private String questioner_name;
private String questioner_sex;
private String comments_allowed;
private String question_id;
private String question_date;
private String is_public;
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getQuestion_answer() {
return question_answer;
}
public void setQuestion_answer(String question_answer) {
this.question_answer = question_answer;
}
public String getQuestion_type() {
return question_type;
}
public void setQuestion_type(String question_type) {
this.question_type = question_type;
}
public String getQuestioner_age() {
return questioner_age;
}
public void setQuestioner_age(String questioner_age) {
this.questioner_age = questioner_age;
}
public String getQuestioner_city() {
return questioner_city;
}
public void setQuestioner_city(String questioner_city) {
this.questioner_city = questioner_city;
}
public String getQuestioner_country() {
return questioner_country;
}
public void setQuestioner_country(String questioner_country) {
this.questioner_country = questioner_country;
}
public String getQuestioner_name() {
return questioner_name;
}
public void setQuestioner_name(String questioner_name) {
this.questioner_name = questioner_name;
}
public String getQuestioner_sex() {
return questioner_sex;
}
public void setQuestioner_sex(String questioner_sex) {
this.questioner_sex = questioner_sex;
}
public String getComments_allowed() {
return comments_allowed;
}
public void setComments_allowed(String comments_allowed) {
this.comments_allowed = comments_allowed;
}
public String getQuestion_id() {
return question_id;
}
public void setQuestion_id(String question_id) {
this.question_id = question_id;
}
public String getQuestion_date() {
return question_date;
}
public void setQuestion_date(String question_date) {
this.question_date = question_date;
}
public String getIs_public() {
return is_public;
}
public void setIs_public(String is_public) {
this.is_public = is_public;
}
}
并解析它的代码。
Gson gson = new Gson();
String jsonString = response.body().string();
Type type = new TypeToken<List<JSONEntity>>() {
}.getType();
List<JSONEntity> datas = gson.fromJson(jsonString, type);
编辑
如果您的回答是JSONArray
,您可以尝试这样做。
List<JSONEntity> datas = response.body();
尝试更改你的JSON
结构
第一种方法
至
如果列为null,则返回"question_type": null
,否则显示"question_type": "value"
代替
如果列为null将返回如此"question_type": []
,if not will return as a string!
的数组
第二种方法不改变Json结构
使用Gson处理Dynamic JSON
试试这个:
您必须使用deserialize
来解析json中的动态数据类型
在回应pojo中使用qazxsw poi
例如:
object
将此 Call<Object> call = //your API call ResponsePojo instead use `Object`
call.enqueue(new Callback<Object>()
{
@Override
public void onResponse(Response<Object> response, Retrofit retrofit)
{
try {
JSONArray jsonArray=new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ServerResponse.class, new ServerResponse.OptionsDeserilizer())
.create();
ServerResponse serverResponse=gson.fromJson(jsonArray.get(i).toString(), ServerResponse.class);
System.out.println(serverResponse);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t)
{
///Handle failure
}
});
与ServerResponsePojo
一起使用
JsonDeserializer
这是快乐的编码工作