Android 预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26472529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:33:30  来源:igfitidea点击:

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

androidgson

提问by Huy

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:

我正在尝试读取和解析以数组(例如[{test: "test"}])开头的 JSON 字符串,但我一直遇到错误:

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?

按照一些 stackoverflow 的答案,我创建了 PayoutCharges 类作为 PayoutCharge 的数组列表。如何解决此问题,以便 GSON 知道 JSON 字符串在数组中?

PayoutCharges.java

支付费用.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.javaso that it knows that the JSON is an array. Something like @SerializedName([])?

阅读@Ridcully 的回复后,我想问一下是否有一种方法可以让我更新,PayoutCharges.java以便它知道 JSON 是一个数组。像@SerializedName([])什么?

回答by Devrim

Below code works for your sample json value:

以下代码适用于您的示例 json 值:

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

您共享的测试示例有一个包含对象的数组。因此,您必须使用 pojo 类的数组,同时将 json 值反序列化为对象(数组)。

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.

如果此答案对您没有帮助(这意味着您的真实 json 值有所不同),您最好分享您正在处理的真实 json。

回答by F. Mark

enter image description hereabove 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 arcticlesolution1:

jsonTemp 是一个字符串,其中包括我在本文解决方案开头列出的内容

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

solution2:

解决方案2:

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

thanks DevrimTuncer.

感谢 DevrimTuncer。

回答by Ridcully

If you define the array of PayoutChargeobjects again as a class (PayoutCharges), your JSON should look something like this:

如果您PayoutCharge再次将对象数组定义为类 ( PayoutCharges),您的 JSON 应如下所示:

{"error" : <... JSONified Error object>,
 "payoutCharges" : [{"test" : "test"}]
}

回答by DONG

Be careful the second parmeter of gson.fromJson(),it needs a Class of array.For example data[].class.

注意gson.fromJson()的第二个参数,它需要一个数组类。例如data[].class。

回答by keinta

That's because the name and type of the second column in your object is in conflict with the parsed data. If you don't have one of the data in the data, you can write it in the last column.

这是因为对象中第二列的名称和类型与解析的数据冲突。如果你没有数据中的一个数据,你可以把它写在最后一列。

image description

图片说明