Reputation: 75
I am having trouble to parse a json array in java.
I have a JSON array like below returning from server
[
{
"User": "538000001",
"Transaction-Id": "oHbgP2y2OXfdDcxAOI/q9HxY68PNs+xS+8CvfGpoN2ZUU/8mavBaI0564VeZXYBDMnk84kkfZeCJM51I92rFdf4Zi4uKEoqJd7jr78bXo4MOyoSs5mntIir7aVJ9/b+4nz6x2+g0LPY7+Sq8RHvbr+c4Evhg+VXeKDzE3f6+bJo=,YWFhd3MxLnJlYWxtMTsxNDE4MDczNTk0MjkxOzUyQDUzODAwMDAwMQ==",
"Challenge-Response": "7ZGlkpVfYvQDjvTa2EShZwZ3dGc=",
"Challenge": "MzcrMzM3NzA4MTM3KzE0MTgwNzM1OTQ="
},
{
"User": "538000000",
"Transaction-Id": "+5Oi4NnG9HOVMPx4nM/TP4ZBONG4HtOBbA5+uf/d+hik7o1Aes9H0PLCqAgG/Td2xLDPOdZJJW7ppj3MLkZBvJr+t9JWKdSGpGHAYTp0oonRTVsesPVCtNI6dXvMY9P+bHDiBWkZiqjSjOZuuzImLaJ17G1/D/GNqIonaNCjqjo=,YWFhd3MxLnJlYWxtMTsxNDE4MDczNTk0Mjk1OzUzQDUzODAwMDAwMA==",
"Challenge-Response": "eEzLzYLmzo5R2tNwokG0mfbuLZY=",
"Challenge": "MzgrNDY2NjY4NjgyKzE0MTgwNzM1OTQ="
}
]
I am using GSON to parse this array but so far not successful.I wrote the following code
class round1Body
{
String User;
String Transaction_Id;
String Challenge_Response;
String Challenge;
round1Body(String User,String Transaction_Id,String Challenge_Response,String Challenge)
{
this.User = User;
this.Transaction_Id=Transaction_Id;
this.Challenge_Response = Challenge_Response;
this.Challenge=Challenge;
}
@Override
public String toString()
{
return "User = " + User + " Transaction-Id = " + Transaction_Id + " Challenge-Response = " + Challenge_Response + "Challenge = "
+ Challenge;
}
}
Type listType = new TypeToken<ArrayList<round1Body>>(){}.getType();
Object jsonE = new Gson().fromJson(firstResponse.readEntity(String.class),listType);
System.out.println(jsonE);
The output when I try to print is [null, null].
Can anybody help me out ?
Thanks in advance !
Upvotes: 1
Views: 450
Reputation: 93842
It seems like you have a problem with the firstResponse.readEntity(String.class)
call. Otherwise you wouldn't have [null, null]
as output. So the problem came from here first. Try to have that working.
Then if you don't specify a SerializedName
rule on your class' fields, GSON requires that the name must match exactly the key's name in your JSON file.
You have "Transaction-Id"
on the JSON side but you defined it as Transaction_Id
in your Java file, same for Challenge_Response
.
Since in Java an identifier can't have a -
, you need to specify a rule for those two attributes.
class round1Body {
String User;
@SerializedName("Transaction-Id")
String Transaction_Id;
@SerializedName("Challenge-Response")
String Challenge_Response;
String Challenge;
//...
}
With these changes, it successfully print (I'm reading your JSON string from a file):
[User = 538000001 Transaction-Id = oHbgP2y2OXfdDcxAOI/q9HxY68PNs+xS+8CvfGpoN2ZUU/8mavBaI0564VeZXYBDMnk84kkfZeCJM51I92rFdf4Zi4uKEoqJd7jr78bXo4MOyoSs5mntIir7aVJ9/b+4nz6x2+g0LPY7+Sq8RHvbr+c4Evhg+VXeKDzE3f6+bJo=,YWFhd3MxLnJlYWxtMTsxNDE4MDczNTk0MjkxOzUyQDUzODAwMDAwMQ== Challenge-Response = 7ZGlkpVfYvQDjvTa2EShZwZ3dGc=Challenge = MzcrMzM3NzA4MTM3KzE0MTgwNzM1OTQ=, User = 538000000 Transaction-Id = +5Oi4NnG9HOVMPx4nM/TP4ZBONG4HtOBbA5+uf/d+hik7o1Aes9H0PLCqAgG/Td2xLDPOdZJJW7ppj3MLkZBvJr+t9JWKdSGpGHAYTp0oonRTVsesPVCtNI6dXvMY9P+bHDiBWkZiqjSjOZuuzImLaJ17G1/D/GNqIonaNCjqjo=,YWFhd3MxLnJlYWxtMTsxNDE4MDczNTk0Mjk1OzUzQDUzODAwMDAwMA== Challenge-Response = eEzLzYLmzo5R2tNwokG0mfbuLZY=Challenge = MzgrNDY2NjY4NjgyKzE0MTgwNzM1OTQ=]
Note that you could use this annotation to respect naming conventions.
Upvotes: 1