Вопрос

I am trying to parse a json using JsonReader. While parsing I came accross 2 scenarios where my app crashes. 1). When JsonArray contains sets of another JsonArrays and and out of which one of it is null. I am giving a scenarios where result is a array and it has 21 more arrays in it and the last array is null.

"results": [(21)
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[(0)]
],

2). When JsonArray has sets of objects in it and one of the object is null. I have attached the example for the same. Here I have source as my JsonArray which contains 21 objects and out of which 1 is null.

"source": [(21)
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
null
]

I am able to check when some individual element within a is null by

reader.peek()== JsonToken.NULL

Error Output in Logcat:

02-04 16:10:02.425: E/AndroidRuntime(1203): FATAL EXCEPTION: pool-1-thread-2
02-04 16:10:02.425: E/AndroidRuntime(1203): java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NULL at line 1 column 327992
02-04 16:10:02.425: E/AndroidRuntime(1203):     at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
02-04 16:10:02.425: E/AndroidRuntime(1203):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)

I cant figure out how can I check whether Array or Object which I want to parse which has no name is Null or not.

Edit:

Code:

public ResultObject parse(){
    try{
        if(reader != null ){
            resultObject    =    new ResultObject();
            reader.beginObject();
            Log.e("123", "inside try ");
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equalsIgnoreCase("id")) {
                    if(reader.peek()== JsonToken.NULL){
                        reader.skipValue();
                        id=    0;
                        Log.e("123", "parse result --------id----------" + id);
                        resultObject.setId(id);
                    }else{
                        id= reader.nextDouble();
                        Log.e("123", "parse result -------id----------" + id);
                        resultObject.setId(id);
                    }
                } else {
                    Log.e("123", "skip value");
                    reader.skipValue();
                }
            }

            reader.endObject();
        }
    }catch(IOException e){ 
        e.printStackTrace();
    }

    return resultObject;
}

Calling function:

if (name.equalsIgnoreCase(SOURCE)) {
    if(reader.peek()== JsonToken.NULL){
        reader.skipValue();
    } else {
        resultObjectList = new ArrayList();
        resultObject    =    new ResultObject();
        reader.beginArray();
        while(reader.hasNext()){
            resultObject    =new ResultJsonUtil(reader).parse();
            if(resultObject!=null){
                resultObjectList.add(resultObject);
            }
        }

        reader.endArray();
    }
}
Это было полезно?

Решение

Your code crashes at reader.beginObject();, it seems. Since there is not an object, Gson throws an exception.

If you're not sure, what comes next, you can test this with peek

Returns the type of the next token without consuming it.

You can then call the proper reader method according to the retrieved JsonToken

if (reader.peek() != JsonToken.BEGIN_OBJECT) {
    // do something
} else {
    // parse object
    resultObject = new ResultObject();
    reader.beginObject();
    ...
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top