-1

Edit I've added the details class and fixed the duplicate key. The dates are in fact strings.

Using gson 2.8 and eclipse.

I managed to get the JSON file parsed and the values stored and printed, but I still get the exception. (BEGIN_OBJECT expected but was STRING at line 1 column 2)

I've checked other solutions, like put an object where expected, but it just throws a different exception. e.g (begin_array is expected)

Json File

[{"numValue1":"1",
"StringValue1":"EI",
"StringValue2":"A",
"enumValue1":"TYPE_A",
"numValue2":"3221",
"StringValue3":"DDEF",
"StringValue4":"Mexicof",
"numValue3":"8",
"stringDate1":"27/05/2017 11:55",
"stringDate2":"29/09/2016 11:55",
"stringDate3":"27/05/2017 11:57"}]

Code used for parsing the file

Gson gson = new GsonBuilder().setDateFormat("dd/MM/yyyy HH:mm").create();
Details[] feedRecord = gson.fromJson(data.trim(), Details[].class);

Details class

public class Details implements Serializable {

enum Type {
    TYPE_D, TYPE_A
};

@SerializedName("numValue1")
private Integer num1;
@SerializedName("StringValue1")
private String string1; 
@SerializedName("StringValue2")
private String string2; 
@SerializedName("numValue2")
private String num2;
@SerializedName("StringValue3")
private String string3;
@SerializedName("StringValue4")
private String string3;
@SerializedName("numValue3")
private String num3;
@SerializedName("StringDate1")
private String date1;
@SerializedName("StringDate2")
private String date2;
@SerializedName("StringDate3")
private String date3;
@SerializedName("enumType1")
private Type enumType;
}

Exception

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $[0]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:852)
    at com.google.gson.Gson.fromJson(Gson.java:801)
    at com.google.gson.Gson.fromJson(Gson.java:773)
  • 1
    How certain are you that the JSON you've shown here is *exactly* the content of `data`? – Jon Skeet Jun 19 '17 at 16:50
  • 3
    Well, you have a duplicate key, I don't know if this causes gson to fail though... – hfhc2 Jun 19 '17 at 16:52
  • 2
    Possible duplicate of ["Expected BEGIN\_OBJECT but was STRING at line 1 column 1"](https://stackoverflow.com/questions/28418662/expected-begin-object-but-was-string-at-line-1-column-1) – bhspencer Jun 19 '17 at 16:52
  • Make sure that the first character in your JSON is not actually an opening quote. – bhspencer Jun 19 '17 at 16:53
  • 1
    We can't see your `Detail` class – OneCricketeer Jun 19 '17 at 16:53
  • `BEGIN_ARRAY` *should be expected*, by the way, as that is what your data is, a list. – OneCricketeer Jun 19 '17 at 16:55
  • 1
    Include the Details class. This is kind of important since gson is throwing the exception based on imput that is supposed to result in an instance of the Details class. – DwB Jun 19 '17 at 16:56
  • I suspect that the `Detail` class fields `Date2` and `Date3` are some kind of date *object*, not `String`, but JSON value is a *string* (e.g. `"29/09/2016 11:55"`), hence the error message `Expected BEGIN_OBJECT but was STRING`. Of course, since you haven't shared the `Detail` class with us, we can only guess. See [Java 8 LocalDateTime deserialized using Gson](https://stackoverflow.com/q/22310143/5221149). – Andreas Jun 19 '17 at 17:23

1 Answers1

0

Using JsonParser, the exception is:(I modified the duplicate key)

Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 252 path $
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559) ~[gson-2.8.0.jar:na]

The Gson api states that the date pattern should follow

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html?is-external=true

This throws no exception:

String str = "[{\"numValue1\":\"1\",\"StringValue1\":\"EI\",\"StringValue2\":\"A\",\"enumValue1\":\"TYPE_A\",\"numValue11\":\"3221\",\"StringValue4\":\"DDEF\",\"StringValue5\":\"Mexicof\",\"numValue2\":\"8\",\"stringDate1\":\"27/05/2017 11:55\",\"Date2\":\"29/09/2016 11:55\",\"Date3\":\"27/05/2017 11:57\"}]\"";
Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy HH:mm").create();
JsonReader jr = gson.newJsonReader(new StringReader(str));


jr.beginArray();
jr.beginObject();
System.out.println(jr.nextName());

Column 252 is where date data begins

efekctive
  • 1,366
  • 2
  • 17
  • 22