java - 缺少名称,处于状态 : START_OBJECT parsing XML using Jackson

标签 java xml jackson

我正在尝试解析一些如下所示的 XML:

<correlationMatrix>
    <assetMatrix numAssets="45">
        <correlations asset="Name1" />
        <correlations asset="Name2">
            <correlation asset="Name3">1.23</correlation>
        </correlations>
        <correlations asset="Name4">
            <correlation asset="Name5">2.34</correlation>
            <correlation asset="Name6">3.45</correlation>
        </correlations>
    </assetMatrix>
</correlationMatrix>

我创建了 3 个类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class CorrelationMatrix {
  private List<Correlations> assetMatrix;

  public List<Correlations> getAssetMatrix() {
    return assetMatrix;
  }

  public void setAssetMatrix(List<Correlations> assetMatrix) {
    this.assetMatrix = assetMatrix;
  }
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Correlations {
 private String asset;
 private List<Correlation> correlation;

 public String getAsset() {
   return asset;
 }

  public void setAsset(String asset) {
    this.asset = asset;
  }

  public List<Correlation> getCorrelation() {
    return correlation;
  }

  public void setCorrelations(List<Correlation> correlation) {
    this.correlation = correlation;
  }

}

然后终于

@JsonIgnoreProperties(ignoreUnknown = true)
public class Correlation {
}

如您所见,我已经从最终内部类中删除了所有内容,但它仍然无法解析。我试过删除 <correlations asset="Name1" />来自输入,但这不是问题的根源。如果我删除 private List<Correlation> correlation;然后从 Correlations 中解析成功,但显然没有我需要的信息。

我在这里需要做些什么不同的事情来使用 Jackson(2.2.0 如果重要的话)将本质上是二维数组的 XML 解析为 Java?

我得到的错误是:

 Missing name, in state: START_OBJECT (through reference chain: CorrelationMatrix["assetMatrix"]->Correlations["correlation"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(

更新:

问题似乎与correlation 中的值有关.如果我从我的示例数据中删除 1.23、2.34 和 3.45,然后它会解析 - 所以我需要以某种方式告诉 Jackson 如何映射它们。

最佳答案

我能够使用这些修改后的类解析示例 xml 中的所有元素(添加 getter、setter 并在 Correlations 中使用正确的名称 setCorrelation):

class CorrelationMatrix {
    private AssetMatrix assetMatrix;
}

class AssetMatrix {
    @JacksonXmlProperty(isAttribute = true)
    private int numAssets;

    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Correlations> correlations;
}

class Correlations {
    @JacksonXmlProperty(isAttribute = true)
    private String asset;

    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Correlation> correlation;
}

class Correlation {
    @JacksonXmlProperty(isAttribute = true)
    private String asset;

    @JacksonXmlText
    private double correlation;
}
  1. 我在任何地方都不需要@JsonIgnoreProperties(ignoreUnknown = true)
  2. @JacksonXmlProperty(isAttribute = true)assetnumAssets 等属性所必需的
  3. xml 中有 2 种类型的列表都已展开,因此请使用此 @JacksonXmlElementWrapper(useWrapping = false)
  4. 指定它
  5. 尽管 Java 中的字段不是文本,但您可以使用此 @JacksonXmlText 解析最里面的双数。
  6. 我引入了一个包装类 AssetMatrix 来捕获 numAssets

关于java - 缺少名称,处于状态 : START_OBJECT parsing XML using Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229975/

相关文章:

java - 拦截一种方法两次

java - 将 JSON 反序列化为类

android - 如何在 Android 中使用位图作为背景从 LinearLayout 中删除黑条?

xml - Scala:修改一个NodeSeq

java - 不匹配的输入异常 : Cannot deserialize instance of `java.lang.Integer` out of START_OBJECT token

java - 将数据格式化为 JSON

java - 在不重启 Activity 的情况下接收自定义 Intent

java - SSL套接字帮助-javax.net.ssl.SSLHandshakeException : Received fatal alert: certificate_unknown

java.sql.SQLException : Unknown type '246 in column 10 of 12 in binary-encoded re sult set 异常

java - Android menuItem setChecked 不起作用