0

Hi I am trying to use GSON class to convert the following Json string.

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

I get this error. com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at com.google.gson.Gson.fromJson(Gson.java:731)

What should be the class hierarchy for this?

Marco Len
  • 13
  • 1
  • 4
  • Possible duplicate of [Why does Gson fromJson throw a JsonSyntaxException: Expected some type but was some other type?](http://stackoverflow.com/questions/33621808/why-does-gson-fromjson-throw-a-jsonsyntaxexception-expected-some-type-but-was-s) – Sotirios Delimanolis Jun 23 '16 at 16:11

1 Answers1

1

Perhaps the following example gives an adequate idea.

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;
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97