首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GSON转换自

GSON转换自
EN

Stack Overflow用户
提问于 2011-12-28 01:10:45
回答 1查看 2.8K关注 0票数 0

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

代码语言:javascript
复制
{"data":
     {"detections":
                  [
                    [ 
                     {"language":"en","isReliable":false,"confidence":0.9759119}
                    ]
                  ]
      }
}

我得到了这个错误。com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在com.google.gson.Gson.fromJson(Gson.java:731)时为BEGIN_OBJECT

它的类层次结构应该是什么?

EN

回答 1

Stack Overflow用户

发布于 2011-12-28 11:26:34

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

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8647260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档