【问题标题】:GSON converting fromGSON 转换自
【发布时间】:2011-12-27 17:10:45
【问题描述】:

您好,我正在尝试使用 GSON 类来转换以下 Json 字符串。

{"data":
     {"detections":
                  [
                    [ 
                     {"language":"en","isReliable":false,"confidence":0.9759119}
                    ]
                  ]
      }
}

我收到此错误。 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为 BEGIN_ARRAY 但为 BEGIN_OBJECT 在 com.google.gson.Gson.fromJson(Gson.java:731)

这应该是什么类层次结构?

【问题讨论】:

标签: json gson


【解决方案1】:

也许下面的例子给出了一个足够的想法。

import java.io.FileReader;
import java.math.BigDecimal;

import com.google.gson.Gson;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Bar bar = gson.fromJson(new FileReader("input.json"), Bar.class);

    System.out.println(bar.data.detections[0][0]);
    // output:
    // Detection: language=en, isReliable=false, confidence=0.9759119
  }
}

class Bar
{
  Data data;
}

class Data
{
  Detection[][] detections;
}

class Detection
{
  Language language;
  boolean isReliable;
  BigDecimal confidence;

  @Override
  public String toString()
  {
    return String.format("Detection: language=%s, isReliable=%s, confidence=%s", language, isReliable, confidence);
  }
}

enum Language
{
  en, fr;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多