1

I'm getting this exception while trying to populate my listview with JsonConverter

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

This is my JsonConverter class :

public class JsonConverter<T> {
public JsonConverter() {
}

public ArrayList<T> toArrayList(String jsonString, Class<T> clazz) {
    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat("dd/MM/yy HH:mm:ss");
    Gson gson = builder.create();
    JsonConverter.ListParameterizedType type = new JsonConverter.ListParameterizedType(clazz);
    ArrayList list = (ArrayList)gson.fromJson(jsonString, type);
    return list;
}

public List<T> toList(String jsonString, Class<T> clazz) {
    ArrayList list = this.toArrayList(jsonString, clazz);
    return list;
}

private static class ListParameterizedType implements ParameterizedType {
    private Type type;

    private ListParameterizedType(Type type) {
        this.type = type;
    }

    public Type[] getActualTypeArguments() {
        return new Type[]{this.type};
    }

    public Type getRawType() {
        return ArrayList.class;
    }

    public Type getOwnerType() {
        return null;
       }
   }
}

This is my model class :

public class Product implements Serializable {

@SerializedName("pid")
public int pid;

@SerializedName("name")
public String name;

@SerializedName("qty")
public int qty;

@SerializedName("price")
public String description;

@SerializedName("image_url")
public String image_url;

@SerializedName("date")
public String date;
}

And I'm calling it like this :

private ArrayList<Product> productList;

@Override
public void  processFinish(String s) {
    productList = new JsonConverter<Product>().toArrayList(s, Product.class);
.
.
.

Don't Know if I did something wrong here

DastakWall
  • 377
  • 1
  • 7
  • 25

2 Answers2

1

The error message tells you that while converting the json to a java object the call expected an array in the json but got a string instead.

expected return:

[
   "product" : {
       "key" : "value"
   }
]

check your json again, it may be a string not an object

klaudiusnaban
  • 196
  • 1
  • 10
  • My data are in mysql database , I download them with AsynkTask then pass them into JsonConverter inside my processFinish method .. I had no problem when I was using Eclipse IDE , I get this error when I use android studio , its weird – DastakWall Apr 05 '17 at 07:44
  • you may need [this](http://stackoverflow.com/questions/16654042/gson-expected-begin-array-but-was-begin-object) – klaudiusnaban Apr 05 '17 at 09:41
0

I had a case where I read from a handwritten json file. The json is perfect. However, this error occurred. So I write from a java object to json file, then read from that json file. things are fine. I could not see any difference between the handwritten json and the one from java object. Tried beyondCompare it sees no difference. I finally noticed the two file sizes are slightly different, and I used winHex tool and detected extra stuff below. So the solution for my situation is, make copy of the good json file, paste content into it and use.

enter image description here

Feng Zhang
  • 1,698
  • 1
  • 17
  • 20