Gson Expected Begin_Array But Was String At Line 1 Column 62

Gson Expected BEGIN_ARRAY but was STRING at line 1 column 62

Your JSON is valid - but your mapping class isn't (parts of it don't match). In particular, the files property of your class cannot be mapped as a Map<String, String> from the given JSON. It's hard to recommend an alternate structure for storing the data without seeing a larger sample, but in general you can follow this guide when mapping between JSON structures and Java classes. This JSON:

"files": [
{
"folder1": "file.txt"
},
{
"folder1/folder2": "file.cfg"
}
]

represents an array containing objects, where each object is best represented as a map. So in essence, a list of maps. Consequently your Java object should be:

public class CFS {
private List<Map<String, String>> files = new ArrayList<Map<String, String>>(
4);
private List<String> directories = new ArrayList<String>(4);

// Constructors, setters/getters
}

Note that I've corrected your properties by making them private and adding getters/setters. With the above defined class your program should work just fine.

final Gson gson = new GsonBuilder().create();
final CFS results = gson.fromJson(json, CFS.class);
Assert.assertNotNull(results);
Assert.assertNotNull(results.getFiles());
System.out.println(results.getFiles());

Produces:

[{folder1=file.txt}, {folder1/folder2=file.cfg}]

If you find yourself needing to retain the current CFS structure though, you would need to manually parse the JSON into it.

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING while using Gson library

Do you verify the json format? I have reformated as below then it works.

     [
{
"heading": "Bike Services",
"content": [
{
"title": "General Service",
"icon": "bike_service_img"
},
{
"title": "Premium Bike Service",
"icon": "bike_service_img"
},
{
"title": "Repair Job",
"icon": "bike_service_img"
},
{
"title": "Bike Ceramic Coating",
"icon": "bike_service_img"
}
]
},
{
"heading": "Bike Service Centre Near You",
"content": [
{
"title": "Bike Shop Name one",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name two",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name three",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name four",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name five",
"icon": "bike_service_centre_img"
},
{
"title": "Bike Shop Name six",
"icon": "bike_service_centre_img"
}
]
},
{
"heading": "Car Services",
"content": [
{
"title": "Express Service",
"icon": "car_service_img"
},
{
"title": "Dent/Scratch Removal",
"icon": "car_service_img"
},
{
"title": "Interior Detailing",
"icon": "car_service_img"
},
{
"title": "Oil Change Package",
"icon": "car_service_img"
},
{
"title": "Complete Car Spa",
"icon": "car_service_img"
},
{
"title": "AC Service",
"icon": "car_service_img"
}
]
},
{
"heading": "Car Service Centre Near You",
"content": [
{
"title": "Car Shop Name one",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name two",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name three",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name four",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name five",
"icon": "car_service_centre_img"
},
{
"title": "Car Shop Name six",
"icon": "car_service_centre_img"
}
]
}
]

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

You state in the comments that the returned JSON is this:

{ 
"dstOffset" : 3600,
"rawOffset" : 36000,
"status" : "OK",
"timeZoneId" : "Australia/Hobart",
"timeZoneName" : "Australian Eastern Daylight Time"
}

You're telling Gson that you have an array of Post objects:

List<Post> postsList = Arrays.asList(gson.fromJson(reader,
Post[].class));

You don't. The JSON represents exactly one Post object, and Gson is telling you that.

Change your code to be:

Post post = gson.fromJson(reader, Post.class);

Expected BEGIN_OBJECT but was STRING at line 1 column 1

Even without seeing your JSON string you can tell from the error message that it is not the correct structure to be parsed into an instance of your class.

Gson is expecting your JSON string to begin with an object opening brace. e.g.

{

But the string you have passed to it starts with an open quotes

"

Expected BEGIN ARRAY but was BEGIN_OBJECT at line 1 and colum2

I have read completely about Retrofit, Retrofit will work when the array is serialized perfectly.

 "offersdata": [        {            "offercode": "GRAB20",            "title": "Get Upto",            "description": "20% off on selected merchandise on purchase of INR 1000 or more"        },        {            "offercode": "JAN20",            "title": "Get Upto",            "description": "20% Off on all purchases in January"        },        {            "offercode": "BHHH",            "title": "DES",            "description": "FDSFS"        }    ],    "message": "success"}

Gson issue:- Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1

Use the second case, but replace

private SubTasks subTasks ;

with

private List<SubTasks> subTasks ;

The clue was in the error.

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 276 path $.subTasks

Given your java classes, it was expecting an object named subTasks but found an array.

So change it to an array and you are gold.

The first case is probably correct, if you end up parsing an array of SMTStatus

GSON throwing Expected BEGIN_OBJECT but was BEGIN_ARRAY?

The problem is you're telling Gson you have an object of your type. You don't. You have an array of objects of your type. You can't just try and cast the result like that and expect it to magically work ;)

The User guide for Gson Explains how to deal with this:

https://github.com/google/gson/blob/master/UserGuide.md

This will work:

ChannelSearchEnum[] enums = gson.fromJson(yourJson, ChannelSearchEnum[].class);

But this is better:

Type collectionType = new TypeToken<Collection<ChannelSearchEnum>>(){}.getType();
Collection<ChannelSearchEnum> enums = gson.fromJson(yourJson, collectionType);

Retrofit2 Android: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

When you say "This code is working with this payload:... but not with this one:..." that's expected and that's how it's suppose to work. In fact the error message tells you that while converting the json to a java object the call expected an array in the json but got an object instead.

This call:

@GET("Music")
Call<List<Music>> getMusicList();

expects a list of Music objects, that's why it works with the json:

[
{
"login": "JakeWharton",
...
},
...
]

Because the json itself is an array of your Music objects (Retrofit can convert between json arrays to java lists). For the second json you have just an object and not an array (notice the lack of [...]). For this you need to create another call with another model that maps to that json. Let's assume you've named the model MusicList. Here's how the call could look like:

@GET("Music")
Call<MusicList> getMusicList();

(Note that you might need to change the method name if you want to keep both the first call and this one).

The MusicList model can look something like this:

public class MusicList {
@SerializedName("data")
private List<Music> musics;
// ...
}

I'm assuming that the data array is a list of Music objects, but I did notice that the jsons are completely different. You might need to adapt this as well, but I think you get the picture here.



Related Topics



Leave a reply



Submit