您好我正在尝试使用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)
这个类的层次结构应该是什么?
答案 0 :(得分: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;
}