Android 改型2,解析json,忽略数组中错误的数据类型

Android 改型2,解析json,忽略数组中错误的数据类型,android,json,gson,retrofit,retrofit2,Android,Json,Gson,Retrofit,Retrofit2,我正在尝试解析来自服务器的json响应。数据是一个对象数组,但有时服务器会在数组项之间发送布尔值。像这样: { "msg_code": 200, "msg_type": "success", "msg_text": "success", "msg_data": [ { "pid": "1234567", "time": "1459029423", "parent_pid": "

我正在尝试解析来自服务器的json响应。数据是一个对象数组,但有时服务器会在数组项之间发送布尔值。像这样:

{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Example {

@SerializedName("msg_code")
@Expose
private Integer msgCode;
@SerializedName("msg_type")
@Expose
private String msgType;
@SerializedName("msg_text")
@Expose
private String msgText;
@SerializedName("msg_data")
@Expose
private List<MsgDatum> msgData = new ArrayList<MsgDatum>();

/**
* 
* @return
* The msgCode
*/
public Integer getMsgCode() {
return msgCode;
}

/**
* 
* @param msgCode
* The msg_code
*/
public void setMsgCode(Integer msgCode) {
this.msgCode = msgCode;
}

/**
* 
* @return
* The msgType
*/
public String getMsgType() {
return msgType;
}

/**
* 
* @param msgType
* The msg_type
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}

/**
* 
* @return
* The msgText
*/
public String getMsgText() {
return msgText;
}

/**
* 
* @param msgText
* The msg_text
*/
public void setMsgText(String msgText) {
this.msgText = msgText;
}

/**
* 
* @return
* The msgData
*/
public List<MsgDatum> getMsgData() {
return msgData;
}

/**
* 
* @param msgData
* The msg_data
*/
public void setMsgData(List<MsgDatum> msgData) {
this.msgData = msgData;
}

}
如您所见,它们之间存在一个

当我试图解析这些时,转换器到达错误的数据类型,并引发如下异常:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 9988 path $.msg_data[12]
那么,我如何跳过这个错误的数据类型并继续解析其他元素呢

这是我创建改装客户端的代码:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(logLevel);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();

        gooderApi = new Retrofit.Builder()
                .baseUrl(BASE_API_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build()
                .create(ApiInterface.class);
我搜索了很多,我知道我必须创建一个定制的转换器工厂,但所有的例子都是旧的,属于翻新<2,我不知道如何让它们为我工作

更新: 类似问题:


提前使用tnx。

在服务器中将调用更改为
JSON对象
删除
false

我想,改装并没有解决这个问题的办法

Bcoz
boolean
进入POJO对象的数组

Boolean
只分配给数据数组

这是正确的json外观

{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
“parent\u pid”之后有
:“0”
您必须删除它


享受编码………

您的
Json
无效,请从中删除

"parent_pid": "0" 

尝试使用和其他站点来检查
JsonObject
是否正确。

建议:将JSON数据放入以检查JSON数据。

删除,检查JSON如下:

{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Example {

@SerializedName("msg_code")
@Expose
private Integer msgCode;
@SerializedName("msg_type")
@Expose
private String msgType;
@SerializedName("msg_text")
@Expose
private String msgText;
@SerializedName("msg_data")
@Expose
private List<MsgDatum> msgData = new ArrayList<MsgDatum>();

/**
* 
* @return
* The msgCode
*/
public Integer getMsgCode() {
return msgCode;
}

/**
* 
* @param msgCode
* The msg_code
*/
public void setMsgCode(Integer msgCode) {
this.msgCode = msgCode;
}

/**
* 
* @return
* The msgType
*/
public String getMsgType() {
return msgType;
}

/**
* 
* @param msgType
* The msg_type
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}

/**
* 
* @return
* The msgText
*/
public String getMsgText() {
return msgText;
}

/**
* 
* @param msgText
* The msg_text
*/
public void setMsgText(String msgText) {
this.msgText = msgText;
}

/**
* 
* @return
* The msgData
*/
public List<MsgDatum> getMsgData() {
return msgData;
}

/**
* 
* @param msgData
* The msg_data
*/
public void setMsgData(List<MsgDatum> msgData) {
this.msgData = msgData;
}

}
将json转换为Pojo后,Pojo类如下所示:

{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
{
    "msg_code": 200,
    "msg_type": "success",
    "msg_text": "success",
    "msg_data": [
        {
            "pid": "1234567",
            "time": "1459029423",
            "parent_pid": "0"
        },
        false,
        {
            "pid": "987654",
            "time": "1458997403",
            "parent_pid": "0"
        }
    ]
}
package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Example {

@SerializedName("msg_code")
@Expose
private Integer msgCode;
@SerializedName("msg_type")
@Expose
private String msgType;
@SerializedName("msg_text")
@Expose
private String msgText;
@SerializedName("msg_data")
@Expose
private List<MsgDatum> msgData = new ArrayList<MsgDatum>();

/**
* 
* @return
* The msgCode
*/
public Integer getMsgCode() {
return msgCode;
}

/**
* 
* @param msgCode
* The msg_code
*/
public void setMsgCode(Integer msgCode) {
this.msgCode = msgCode;
}

/**
* 
* @return
* The msgType
*/
public String getMsgType() {
return msgType;
}

/**
* 
* @param msgType
* The msg_type
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}

/**
* 
* @return
* The msgText
*/
public String getMsgText() {
return msgText;
}

/**
* 
* @param msgText
* The msg_text
*/
public void setMsgText(String msgText) {
this.msgText = msgText;
}

/**
* 
* @return
* The msgData
*/
public List<MsgDatum> getMsgData() {
return msgData;
}

/**
* 
* @param msgData
* The msg_data
*/
public void setMsgData(List<MsgDatum> msgData) {
this.msgData = msgData;
}

}
package.com.example;
导入java.util.ArrayList;
导入java.util.List;
导入javax.annotation.Generated;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
@生成(“org.jsonschema2pojo”)
公开课范例{
@SerializedName(“msg_代码”)
@暴露
私有整数msgCode;
@SerializedName(“消息类型”)
@暴露
私有字符串msgType;
@SerializedName(“消息文本”)
@暴露
私有字符串msgText;
@SerializedName(“msg_数据”)
@暴露
私有列表msgData=newarraylist();
/**
* 
*@返回
*msgCode
*/
公共整数getMsgCode(){
返回msgCode;
}
/**
* 
*@param msgCode
*msg_代码
*/
公共无效setMsgCode(整数msgCode){
this.msgCode=msgCode;
}
/**
* 
*@返回
*msgType
*/
公共字符串getMsgType(){
返回msgType;
}
/**
* 
*@param msgType
*msg_类型
*/
公共void setMsgType(字符串msgType){
this.msgType=msgType;
}
/**
* 
*@返回
*msgText
*/
公共字符串getMsgText(){
返回msgText;
}
/**
* 
*@param msgText
*msg_文本
*/
公共void setMsgText(字符串msgText){
this.msgText=msgText;
}
/**
* 
*@返回
*姆斯格达塔酒店
*/
公共列表getMsgData(){
返回msgData;
}
/**
* 
*@param msgData
*msg_数据
*/
公共无效设置msgData(列表msgData){
this.msgData=msgData;
}
}
试试这个:

    try {
            JSONObject jsonObject = new JSONObject(yourJson);
            JSONArray jsonArray = jsonObject.getJSONArray("msg_data");
            for(int i=0;i<jsonArray.length();i++) {
                if (jsonArray.get(i) instanceof JSONObject) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                } else {
                    Boolean b = jsonArray.getBoolean(i);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
试试看{
JSONObject JSONObject=新的JSONObject(yourJson);
JSONArray JSONArray=jsonObject.getJSONArray(“msg_数据”);

对于(int i=0;isorry这是我的错误,我只是省略了一些用于使代码简短的数据。实际问题是布尔值。它对我的类无效,您建议将响应类型设置为
Object
,我有我的自定义类。请查看错误。很抱歉,这是我的错误,我只是省略了一些用于使代码简短的数据。实际的p问题是布尔值。是的,布尔值不应该存在,你应该在
JsonObject
中使用
JsonArray
数据来自第三方服务器,我无法修复它,我需要在我的代码中解决这个问题。@DanielK我在哪里可以做到?改装完成所有工作。我对这个问题很感兴趣,你是否将POJO架构更新为是的,如果布尔值不存在,我的POJO类是全功能的。当数据类型正确时,我的解析器工作正常。这是一种特殊情况,有时服务器在数组中放入布尔值,使我的解析中断。你做得比我好,经过这么长时间,我找不到这个问题的好参考。我知道现在,这是一个解决方案,但您会考虑,因为这只是偶尔发生的,在异常块中重新调用API调用?它可能比创建自定义反序列化器容易。“代码>重做异常块中的API调用是什么意思?< /代码>对不起,<代码>,<代码>是一个打印错误。请检查您的Web服务是否需要B。OOLANE或不?如果不丢弃它,我不需要它,问题是如何跳过它。在解析它时,你不能跳过响应类型,你必须根据你的需要来改变你的Web服务。下面的问题是,它们有创建自定义转换器的方法,我只需要把它应用到我的情况,但是我不知道如何。se json,但我使用的是改型。我如何采用这个来改型?类似的问题: