1
votes

Now a days I'm having experience of Json parsing .Not have much practice with collection.I have a Json String

{   
    "time":1352113682,
    "api_version":"1",
    "firstname":"abc",
    "lastname":"xyz",
    "company":"Cool Apps",
    "email":"[email protected]"
}

I made class

public class AuthenticateUser implements Serializable{

    // Response when Successfully Login
    public String time;
    public String api_version;
    public String firstname;
    public String lastname;
    public String company;
    public String email;

}

And trying to parse it like this

Map<String, AuthenticateUser> map=null;
ObjectMapper mapper=new ObjectMapper();
try{
    map=mapper.readValue(result,new TypeReference<Map<String, AuthenticateUser>>(){});
    Set<String> keys=map.keySet();
    for (String key : keys) {
    System.out.println(map.get(key).time);
    System.out.println(map.get(key).api_version);
        System.out.println(map.get(key).firstname);
    System.out.println(map.get(key).lastname);
    System.out.println(map.get(key).company);
    System.out.println(map.get(key).email);
    }
}catch (Exception e) {
    e.printStackTrace();
}

But gettin this error

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class AuthenticateUser] from JSON integral number; no single-int-arg constructor/factory method at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromInt(StdValueInstantiator.java:316) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromNumber(BeanDeserializer.java:427) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:119) at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:429) at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:310) at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2577) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1817) at Driver$1.run(Driver.java:140)

1

1 Answers

0
votes

The example you gave at the beginning is for a single AuthenticateUser object. Is that all that is being passed to this program as input? This absolutely will not parse properly.

A single AuthenticateUser is not a Map of type (String, AuthenticateUser)

I think maybe you're confused, why are you making a map? If you intended to parse only a single object, all you need is:

AuthenticateUser user =mapper.readValue(result, AuthenticateUser.class);

System.out.println(user.time);

How does that look? Are you passing a map in JSON?