杰克逊解析JSON数组

杰克逊解析JSON数组

本文介绍了java-杰克逊解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析方面的帮助,我尝试创建其他类型的模型类,但是没有用,请在这里帮助我. json看起来像这样:

I need help with parsing, I've tried to create a different kind of model classes, but no use, please help me out here. the json looks like this:

[
  [
    1518909300000,
    "0.08815700",
    "0.08828700",
    "0.08780000",
    "0.08792900",
    "1727.93100000",
    1518910199999,
    "152.11480375",
    5118,
    "897.71600000",
    "79.04635703",
    "0"
  ],
  [
    1518910200000,
    "0.08788400",
    "0.08824200",
    "0.08766200",
    "0.08810000",
    "1789.81300000",
    1518911099999,
    "157.20177729",
    6201,
    "898.89500000",
    "78.95697080",
    "0"
  ]
]

并且我正在尝试使用数据类来解析它:

and I'm trying to parse it using data class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class KlineResponse {

    public List<Kline> getKlineList() {
        return klineList;
    }

    public List<Kline> klineList;

    public class Kline {
        @JsonProperty("4")
        Double close;

        @JsonProperty("8")
        Integer tradesNumber;

        public Double getClose() {
            return close;
        }

        public void setClose(Double close) {
            this.close = close;
        }

        public Integer getTradesNumber() {
            return tradesNumber;
        }

        public void setTradesNumber(Integer tradesNumber) {
            this.tradesNumber = tradesNumber;
        }
    }
}

和这行

mapper.readValue(response.getBody(), new TypeReference<List<KlineResponse>>(){})

mapper.readValue(response.getBody(), KlineResponse.class)

但是每次都会出现错误:无法从START_ARRAY令牌中反序列化pt.settings.model.KlineResponse实例,请帮助

but each time the error:Can not deserialize instance of pt.settings.model.KlineResponse out of START_ARRAY token,please help

推荐答案

核心问题是您收到期望的数组数组和对象数组.将mapper.readValue(response.getBody(), KlineResponse.class)更改为mapper.readValue(response.getBody(), Object[].class)即可确认.

The core issue is that you receive an array of arrays where you expect and array of objects. Changing mapper.readValue(response.getBody(), KlineResponse.class) to mapper.readValue(response.getBody(), Object[].class) confirms it.

关于如何进行操作,您有两种选择:

You have a couple of options on how to proceed:

  1. 按照@ cricket_007在他的答案
  2. 上的建议,从Jackson更改为标准JSON解析.
  3. 不是将其映射到对象,而是尝试以不同的方式访问JSON.有关示例,请参见@jschnasse的答案.
  4. 如果可以,请更改您解析的文本格式
  5. 如果您无法更改输入格式,则可以
    • 创建一个构造函数,并使用@JsonCreator对其进行注释,如指示此处
    • 将输入解析为对象数组,并将解析后的数组馈入您自己的构造函数中
  1. Change from Jackson to standard JSON parsing, as suggested by @cricket_007 on his answer
  2. Instead of mapping it to an object try to access the JSON differently. See @jschnasse's answer for an example.
  3. Change the format of text you parse, if you can
  4. If you can't change the format of the input then you can either
    • Create a constructor and annotate it with @JsonCreator, like instructed here
    • Parse the input as Object array and feed the parsed array into a constructor of your own

这篇关于java-杰克逊解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:25