Android JSon error "BEGIN_OBJECT expected, but BEGIN_ARRAY in row 1 of column 2"

I get JSon data from a web service, example data is shown below:

[ { "SectionId": 1, "SectionName": "Android" } ] 

When I try to convert it, it throws an error, I do it like:

 Data data = new Gson().fromJson(jsonDataFromWebService, Data.class); 

My section class:

 class Section { public int SectionId; public String SectionName; } class Data { public List<Section> sections; } 

LogCat says:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT expected, but BEGIN_ARRAY in row 1 of column 2

+6
source share
3 answers

The error explains what is wrong ... ur returns an array, not a JSon object

try the following:

 JSONArray ja = new JSONArray(jsonStringReturnedByService); Data sections = new Data(); for (int i = 0; i < ja.length(); i++) { Section s = new Section(); JSONObject jsonSection = ja.getJSONObject(i); s.SectionId = Integer.ValueOf(jsonSection.getString("SectionId")); s.SectionName = jsonSection.getString("SectionName"); //add it to sections list sections.add(s); } return sections; 
+6
source

You are trying to create an object without an array (Collection) from a JSONArray. The error is pretty clear: GSON was expecting the beginning of the object, but instead found the beginning of the array.

Take a look at the documentation page below to learn how to work with arrays and collection types using GSON

https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

From the docs:

Array Examples

Gson gson = new Gson (); int [] ints = {1, 2, 3, 4, 5}; String [] strings = {"abc", "def", "ghi"};

(Serialization) gson.toJson (ints); ==> fingerprints [1,2,3,4,5] gson.toJson (strings); ==> prints ["abc", "def", "ghi"]

(Deserialization) int [] ints2 = gson.fromJson ("[1,2,3,4,5]", INT [] class). ==> ints2 will be the same as ints

We also support multidimensional arrays with arbitrarily complex element types. Collection examples

Gson gson = new Gson (); Collection ints = Lists.immutableList (1,2,3,4,5);

(Serialization) String json = gson.toJson (ints); === json is [1,2,3,4,5]

(Deserialization) type collectionType = new TypeToken> () {} GetType (). Collection ints2 = gson.fromJson (json, collectionType); ints2 is the same as ints

Pretty disgusting: note how we determine the type of collection Unfortunately, there is no way around this in Java

Collection Limitations

It can serialize a collection of arbitrary objects, but cannot deserialize from it. Because there is no way to tell the user the type of the resulting object. Upon deserialization, the collection must have a specific generic type. All of this makes sense and is rarely a problem w> if you follow good Java coding practices.

+8
source

Use the class class as follows:

 Section[] sectionArray = new Gson().fromJson(jsonDataFromWebService, Section[].class); for (Section section: sectionArray) { Log.e("Debug", section.toString()); } 
0
source

Source: https://habr.com/ru/post/909332/


All Articles