I want to serialize this JSON structure :
{
"name":"name 1",
"number":1,
"standing":[
{
"subRank":1,
"subname":"test"
},
{
"subRank":2,
"subname":"Test2"
}]
}
And I want to use an object association like that :
public class ParentClass{
String name;
String number;
List<SubClass> SubClassList;
}
public class SubClass{
String subRank;
String subname;
}
I tried with this code :
Type type = new TypeToken<List<ParentClass>>() {}.getType();
ArrayList<ParentClass> parentClassList= new ArrayList<>();
parentClassList= gson.fromJson(jsonContent, type);
But i have this exception :
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Thanks for your helps :)
First of all, your json structure is not valid.
Remove last comma from below
"subname":"Test2",
And your json structure is not a list, it is object structure and you try to parse it as list.
Change your code as:
ParentClass parentClassList= gson.fromJson(data.get(0), type);
Or change your json structure to array wrapping with "[]" symbols.
This works on me :
Gson gson = new Gson();
Type type = new TypeToken<ParentClass>() {}.getType();
ParentClass parentClassList= gson.fromJson(data, type);
System.out.println(parentClassList.name);
Json:
{"name":"name 1","number":1,"standing":[{"subRank":1,"subname":"test"},{"subRank":2,"subname":"Test2"}]}
The approach that you are trying is for deserialize a collection ( here you can read a very good example). But this is not your case.
If you want to serialize this json:
{
"name":"name 1",
"number":1,
"standing":[
{
"subRank":1,
"subname":"test"
},
{
"subRank":2,
"subname":"Test2"
}]
}
The structure of your classes is not the correct one.
First of all, your number
and subRank
are not String
.
Secondly, this one List<SubClass> SubClassList;
have to be called standing
.
So, the parent class will be:
public class ParentClass {
String name;
Number number;
Collection<SubClass> standing;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Number getNumber() {
return number;
}
public void setNumber(Number number) {
this.number = number;
}
public Collection<SubClass> getStanding() {
return standing;
}
public void setStanding(Collection<SubClass> standing) {
this.standing = standing;
}
}
And the sub class:
public class SubClass {
Number subRank;
String subname;
public Number getSubRank() {
return subRank;
}
public void setSubRank(Number subRank) {
this.subRank = subRank;
}
public String getSubname() {
return subname;
}
public void setSubname(String subname) {
this.subname = subname;
}
}
If you want to test:
public class Test {
public static void main (String args[])
{
Gson gson = new Gson();
SubClass sc = new SubClass();
sc.setSubRank(1);
sc.setSubname(("test"));
SubClass sc2 = new SubClass();
sc2.setSubRank(2);
sc2.setSubname(("Test2"));
LinkedList<SubClass> list= new LinkedList<>();
list.add(sc);
list.add(sc2);
ParentClass pc = new ParentClass();
pc.setName("name 1");
pc.setNumber(1);
pc.setStanding(list);
System.out.println(gson.toJson(pc));
}
}
I did very similiar things with GSON.
Example below. Hope it points you in the right direction...
public class ParentClass{
String name;
String number;
@SerializedName("standing")
SubClass[] SubClassList;
}
public class SubClass{
String subRank;
String subname;
}
I resolve my case like that :
JSON :
{
"name":"name 1",
"number":1,
"standing":[
{
"subRank":1,
"subname":"test"
},
{
"subRank":2,
"subname":"Test2"
}]
}
POJO :
public class ParentClass{
String name;
int number;
List<SubClass> standing;
}
public class SubClass{
int subRank;
String subname;
}
JAVA :
Gson gson = new Gson();
Type type = new TypeToken<ParentClass>() {}.getType();
ParentClass parentClassList= gson.fromJson(data, type);
Thanks very much @Emre, @Christoph-Tobias Schenke and @Davide Patti
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.