Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Tags:

android

gson

I am trying to read and parse a JSON string which starts as an array (e.g. [{test: "test"}]) and I keep running into the error:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

The error in my log points to this line:

Gson gson = new GsonBuilder().create();
PayoutCharges payoutList = gson.fromJson(reader, PayoutCharges.class);

Following some stackoverflow answers, I created the PayoutCharges class as an array list of PayoutCharge. How do I fix this issue so that GSON knows that the JSON string is inside an array?

PayoutCharges.java

package com.app.driver.entity;

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;

public class PayoutCharges {
    //handle error
    @SerializedName("error")
    private Error mError;

    public Error getError() {
        return mError;
    }

    public void setError(Error error) {
        mError = error;
    }

    //handle data
    @SerializedName("payoutCharges")
    private ArrayList<PayoutCharge> mPayoutCharges;

    public ArrayList<PayoutCharge> getPayoutCharges() {
        return mPayoutCharges;
    }

    public void setPayoutCharges(ArrayList<PayoutCharge> payoutCharges) {
        mPayoutCharges = payoutCharges;
    }
}

After reading @Ridcully's response, I want to ask if there is a way for me to update PayoutCharges.java so that it knows that the JSON is an array. Something like @SerializedName([])?

like image 298
Huy Avatar asked Oct 20 '14 18:10

Huy


3 Answers

Below code works for your sample json value:

String val1 = "[{test: \"test\"}]";

final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();

TestCase[] testCase = gson.fromJson(val1, TestCase[].class);

The TestCase holder class:

private static class TestCase {
    @SerializedName("test")
    private String field;
}

The test example you've shared has an array which has objects. So you have to use an array of your pojo class while deserializing the json value to an object(array).

If this answer does not help you (which means you have something different on your real json value), you should better share the real json that you are working on.

like image 154
Devrim Avatar answered Nov 12 '22 05:11

Devrim


enter image description here above is what I need to parse; here is my entity:

**package com.winway.ecloud.data.entity;

import java.util.List;


public class WellSectionEntity {


        private String name;    

        private String picture;

        private String remark;  

        private List<Point> pos;    

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPicture() {
            return picture;
        }
        public void setPicture(String picture) {
            this.picture = picture;
        }
        public String getRemark() {
            return remark;
        }
        public void setRemark(String remark) {
            this.remark = remark;
        }
        public List<Point> getPos() {
            return pos;
        }
        public void setPos(List<Point> pos) {
            this.pos = pos;
        }

}
package com.winway.ecloud.data.entity;


public class Point {
    public float x;// 
    public float y;// 
    public float circleR = 50;//
    public float r;// 
    private String lineNO;// 
    private String lineName;// 
    private String no;// 
    private int deep;// 


    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getR() {
        return r;
    }

    public float getCircleR() {
        return circleR;
    }

    public void setCircleR(float circleR) {
        this.circleR = circleR;
    }

    public void setR(float r) {
        this.r = r;
    }

    public String getLineNO() {
        return lineNO;
    }

    public void setLineNO(String lineNO) {
        this.lineNO = lineNO;
    }

    public String getLineName() {
        return lineName;
    }

    public void setLineName(String lineName) {
        this.lineName = lineName;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String msg) {
        this.no = msg;
    }

    public int getDeep() {
        return deep;
    }

    public void setDeep(int deep) {
        this.deep = deep;
    }
}

final GsonBuilder gsonBuilder = new GsonBuilder();
        final Gson gson = gsonBuilder.create();

jsonTemp is a String, which include what i list at the head of this this arcticle solution1:

WellSectionEntity[] listSection = gson.fromJson(***jsonTemp***, WellSectionEntity[].class);

solution2:

List<WellSectionEntity> sectionlist = gson.fromJson(jsonTemp, new TypeToken<List<WellSectionEntity>>(){}.getType());

thanks DevrimTuncer.

like image 4
F. Mark Avatar answered Nov 12 '22 05:11

F. Mark


You can solve this problem in Retrofit with a simple change in Api Interface. You just need to edit your Call<>.

Before:

Call

Edit this into:

Call<List>

Just encode the response in a list, and your problem will be solved.

like image 2
Programmer Tony Avatar answered Nov 12 '22 07:11

Programmer Tony