1

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 !

1
  • Before you try to use a fancy "POJO" parser, learn how to deal with JSON as simple Maps and Lists. The concepts are very simple. Commented Dec 8, 2014 at 22:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you very much for the response. I just used System.out.println(firstResponse.readEntity(String.class)); and it printed correctly but I am still getting same [null,null] :( Maybe problem with the listType ?? Type listType = new TypeToken<ArrayList<round1Body>>(){}.getType(); ??
Did you use same listType I used ?? in new Gson().fromJson(firstResponse.readEntity(String.class),listType);
@shbolise Yes, I copy pasted your code. The only difference is that I'm reading the json data from a file, that's why I suspect firstResponse.readEntity(String.class) is the problem.
String json = firstResponse.readEntity(String.class); System.out.println(json); // Works Fine Object jsonE = new Gson().fromJson(json,listType); System.out.println(jsonE); // [null,null] .... I must be doing some thing stupidly wrong :(
@shbolise What do you mean by work fine? What is the content of json? What happens if you put the json in a file and try to read it from there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.