mehdok
mehdok

Reputation: 1608

Retrofit 2, parsing json, ignore wrong data type inside array

i'm trying to parse a response json from server. the data is an array of objects, but some times server send an boolean between items of array. like this:

{
    "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"
        }
    ]
}

as you can see there is a false between them.

when i try to parse these the the converter reach the wrong data type and throw an exception like these:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 9988 path $.msg_data[12]

so how can i skip this wrong datatype and continue to parse other elements?

this is my code for creating Retrofit client:

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);

i searched alot and i know i must create a custom converter factory, but all the example are old and belongs to Retrofit < 2, and i don't know how to make them work for me.

Update: similar question: GSON ignore elements with wrong type

tnx in advance.

Upvotes: 3

Views: 2630

Answers (5)

sushildlh
sushildlh

Reputation: 9056

Change call into JSON Object OR remove false in the server.

Retrofit didn't have solution for that i think.....

Bcoz boolean coming into the array of POJO object.

Boolean is just assign to array of data.

json

this is correct json have look

{
    "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"
        }
    ]
}

there is , after "parent_pid": "0" you have to remove it .

enjoy coding.........

Upvotes: 1

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Try this:

    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();
        }

Upvotes: 0

Lips_coder
Lips_coder

Reputation: 686

Remove the , check the json like this:

{
    "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"
        }
    ]
}

After converting your json to Pojo your Pojo class look like this:

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;
}

}

Upvotes: 0

eralp
eralp

Reputation: 113

Suggestion: Put your JSON data into Json Parser Online to check your JSON data.

Upvotes: 0

Ravi
Ravi

Reputation: 35569

Your Json is invalid, remove , from

"parent_pid": "0" 

Try to use Json formatter & Validator,JsonLint and other sites to check whether JsonObject is correct or not.

Upvotes: 0

Related Questions