【问题标题】:Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13预期为 BEGIN_OBJECT,但在第 1 行第 13 列是 BEGIN_ARRAY
【发布时间】:2016-07-05 14:55:07
【问题描述】:

我有这种格式的 Json 字符串。

{
    "columns": ["One", "Two", "Three"],
    "columnNames": ["One", "Two", "Three"],
    "isCTemplate": [true, true, true],
    "isCrequired": [false, false, false],
    "columnSizes": [10, 50, -1],
    "columnFormats": ["TEXT", "TEXT", "BOOLEAN(true/false)"],
    "parseErrors": null,
    "data": [
        ["a", "f", "8"],
        ["b", "e", "82"],
        ["c", "d", "822"]
    ],
    "numberOfRecordsInserted": 0,
    "dataErrors": [null, null, null],
    "dataWarnings": ["1", "2", "3"],
    "templateCH": ["One", "Two", "Three"],
    "importS": [null, null, null],
    "formatTypeMap": {
        "array": "TEXT",
        "boolean": "BOOLEAN(true/false)",
        "currency": "CURRENCY"
    },
    "TypeId": "acc",
    "isColumn": [false, false, false],
    "prefixed": [],
    "missed": "tEST",
    "duplicateS": null,
    "global": null,
    "y": 0,
    "s": 1,
    "m": 2,
    "f": 3,
    "vStatus": {
        "vRows": 5,
        "v": 5
    }
}

我的 Jsonhelper 类,

public class CopyOfTViewsJSONS {
    @JsonProperty("columns")
    public List<String> columns=new ArrayList<String>();

    @JsonProperty("columnNames")
    public List<String> columnNames=new ArrayList<String>();

    @JsonProperty("isCTemplate")
    public List<String> isCTemplate=new ArrayList<String>();

    @JsonProperty("isCrequired")
    public List<String> isCrequired=new ArrayList<String>();

    @JsonProperty("columnSizes")
    public List<String> columnSizes=new ArrayList<String>();

    @JsonProperty("columnFormats")
    public List<String> columnFormats=new ArrayList<String>();

    @JsonProperty("parseErrors")
    public List<String> parseErrors=new ArrayList<String>();

    @JsonProperty("data")
    public List<List<String>> data=new ArrayList<List<String>>();

    @JsonProperty("numberOfRecordsInserted")
    public int numberOfRecordsInserted;

    @JsonProperty("dataErrors")
    public List<String> dataErrors=new ArrayList<String>();

    @JsonProperty("dataWarnings")
    public List<String> dataWarnings=new ArrayList<String>();

    @JsonProperty("templateCH")
    public List<String> templateCH=new ArrayList<String>();

    @JsonProperty("importS")
    public List<String> importS=new ArrayList<String>();

    @JsonProperty("formatTypeMap")
    public HashMap<String, String> formatTypeMap=new HashMap<String, String>();

    @JsonProperty("TypeId")
    public String TypeId;

    @JsonProperty("prefixed")
    public List<String> prefixed=new ArrayList<String>();

    @JsonProperty("missed")
    public List<String> missed=new ArrayList<String>();

    @JsonProperty("duplicateS")
    public List<String> duplicateS=new ArrayList<String>();

    @JsonProperty("global")
    public List<String> global=new ArrayList<String>();

    @JsonProperty("Y")
    public int Y;

    @JsonProperty("s")
    public int s;

    @JsonProperty("m")
    public int m;

    @JsonProperty("f")
    public int f;

    @JsonProperty("vStatus")
    public HashMap<String, String> vStatus=new HashMap<String, String>();


    }

我正在尝试以这种方式将该 json 字符串分配给 HashMap&lt;String,CopyOfTViewsJSONS&gt;

HashMap<String,CopyOfTViewsJSONS> mapsss = new Gson().fromJson(tmp, new TypeToken<HashMap<String, CopyOfTViewsJSONS >>(){}.getType());

但我收到java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13&lt;br /&gt;com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13 Exception.

我是第一次使用带有 gson 库的 json。

谁能帮我解决这个问题?

谢谢

【问题讨论】:

  • 试试这个Gson gson = new GsonBuilder().setLenient().create();
  • @Matthew 需要在分配给 hashmap 之前添加这个?
  • 你可以用它代替new Gson().fromJson(...而不是gson.fromJson(tmp, ...
  • 过去将其设置为 lenient 对我有所帮助,但 go to this thread if it doesn't.
  • @Matthew setLenient 对于我得到的 GsonBuilder 未定义

标签: java json hashmap gson


【解决方案1】:

您为什么要尝试将 JSON 反序列化为 Map&lt;String, CopyOfTViewsJSONS&gt;?据我从课程和您发布的 JSON 中可以看出,JSON 完美地代表了 CopyOfTViewsJSONS 类的一个实例。因此,您应该能够像这样反序列化它:

CopyOfTViewsJSONS result = new Gson().fromJson(tmp, CopyOfTViewsJSONS.class);

但是,这样做有一个小问题,因为 "missed" 属性在您的类中被错误地映射。在您的 JSON 示例中,"missed"String,但在类中它是 List&lt;String&gt;。将类字段更改为 String 可以解决此问题:

@JsonProperty("missed")
public String missed;

然后您可以打印出结果的一些属性以确认它已正确反序列化:

System.out.println(result.columns);                  // prints [One, Two, Three]
System.out.println(result.numberOfRecordsInserted);  // prints 0
System.out.println(result.TypeId);                   // prints acc
System.out.println(result.vStatus);                  // prints {v=5, vRows=5}

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多