-6

I am trying to parse a JSON without any key except the object.

For example:

[
  {
    "OBJECT1":{
                 ....
              }
  },
     "OBJECT2":{
                 ....
                }
  }
]

In this case i am trying to parse this JSON like a List objects, and the class Objects include the values inside these objects. I don't know if i am doing right.

The error response of Gson is

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

I am parsing the JSON correctly??

  • 3
    Possible duplicate of [Why does Gson fromJson throw a JsonSyntaxException: Expected some type but was some other type?](https://stackoverflow.com/questions/33621808/why-does-gson-fromjson-throw-a-jsonsyntaxexception-expected-some-type-but-was-s) and this https://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array – AskNilesh Apr 19 '18 at 11:24
  • Post your code, the way you are parsing it – Jyot Apr 19 '18 at 11:25
  • 1
    You are trying to parse JSONObject but it is actually a JSONArray, So try to parse JSONArray first. – Onkar Nene Apr 19 '18 at 11:25
  • you are parsing it incorrectly. You need to fix your code. – Vladyslav Matviienko Apr 19 '18 at 11:59

2 Answers2

1

You can using Gson library. if we assume your object is a person class like this

class Person{
String name;
int age;
}

and your json response like this

[
   {
      "OBJECT1":{
         "name":"mohamed",
         "age":21
      },
      "OBJECT2":{
         "name":"shalan",
         "age":21
      }
   }
]

you need to create a class to reflect each object in your json for Object1

public class OBJECT1 {

@SerializedName("name")
@Expose
private String name;
@SerializedName("age")
@Expose
private Integer age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

and for your Object2

public class OBJECT2 {

@SerializedName("name")
@Expose
private String name;
@SerializedName("age")
@Expose
private Integer age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

and this is a person class for holding your objects

public class Person {

@SerializedName("OBJECT1")
@Expose
private OBJECT1 oBJECT1;
@SerializedName("OBJECT2")
@Expose
private OBJECT2 oBJECT2;

public OBJECT1 getOBJECT1() {
return oBJECT1;
}

public void setOBJECT1(OBJECT1 oBJECT1) {
this.oBJECT1 = oBJECT1;
}

public OBJECT2 getOBJECT2() {
return oBJECT2;
}

public void setOBJECT2(OBJECT2 oBJECT2) {
this.oBJECT2 = oBJECT2;
}

}

to convert your json array to a list of your object List

Gson gson = new Gson();
String jsonOutput = "your json result is here";
Type listType = new TypeToken<List<Person>>(){}.getType();
List<Persopn> persons = (List<Person>) gson.fromJson(jsonOutput, listType);
Shalan93
  • 774
  • 1
  • 7
  • 20
-1

Looks like syntax error, try this:-

[
  {
    "OBJECT1":[            {le:"Dashobard",level:"01"},{name:"About Us",level:"02"},{name:"Contact Us",level:"03"},{name:"Dashobard",level:"04"}
              ]
  },
   {  "OBJECT2":[            {le:"Dashobard",level:"01"},{name:"About Us",level:"02"},{name:"Contact Us",level:"03"},{name:"Dashobard",level:"04"}
              ]
  }
]
vaishali
  • 9
  • 2