0

I want to desalinize following JSON object to Java Object (Location Class) :

{
  "city": "TEST",
  "country": "TEST",
  "latitude": "1",
  "longitude": "1",
  "name": "TEST",
  "postalCode": "362001",
  "street": "TEST",
  "organization": 3
}

My Java Class is :

public class Location{
@ManyToOne
@ForeignKey(name = "FK_location__organization")
private Organization organization;

@NotNull
@Column(nullable = false)
private String name;

private boolean disabled;

private String street;
private String postalCode;
private String city;
private String country;

private Double latitude;
private Double longitude;

@Override
public String toString()
{
    return name;
}

public static List<Location> findAllLocationsOrderedByName()
{
    return entityManager().createQuery("SELECT o FROM Location o ORDER BY name ASC", Location.class).getResultList();}}

And, Organization Class is :

public class Organization {
String name
}

I got following error when trying to deserialize :

flexjson.JsonNumber cannot be cast to java.util.Map
flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17)
flexjson.ObjectBinder.bind(ObjectBinder.java:95)
flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:149)
flexjson.ObjectBinder.bind(ObjectBinder.java:95)
flexjson.ObjectBinder.bind(ObjectBinder.java:74)
flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158)
flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38)

I use following code (using Flex JSON) for deserializing :

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public static Location Location.fromJsonToLocation(String json) {
    System.out.println(json);
    return new JSONDeserializer<Location>()
    .use(null, Location.class).deserialize(json);
}
Brian
  • 3,850
  • 3
  • 21
  • 37
Devloper
  • 120
  • 3
  • 14
  • 4
    So? What's the question? – Edwin Dalorzo Dec 31 '14 at 12:40
  • I want to Deserialize that Json Object in to Java Class.. when i tried i got following error flexjson.JsonNumber cannot be cast to java.util.Map flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:17) flexjson.ObjectBinder.bind(ObjectBinder.java:95) flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:149) flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:38) flexjson.ObjectBinder.bind(ObjectBinder.java:95) flexjson.ObjectBinder.bind(ObjectBinder.java:74) flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:158) – Devloper Dec 31 '14 at 12:42
  • 1
    Please update the question with this error. Also please add what tool are you using to parse JSON. – mvg Dec 31 '14 at 12:44
  • Don't you think it should have been relevant to show us what you did not just what you want? In the question not in cumbersome and hard to read comments would be much better. – Edwin Dalorzo Dec 31 '14 at 12:44
  • Please add the code you have used to parse JSON in the question. – mvg Dec 31 '14 at 12:46
  • public static Location Location.fromJsonToLocation(String json) { System.out.println(json); return new JSONDeserializer() .use(null, Location.class).deserialize(json); } – Devloper Dec 31 '14 at 12:49
  • 1
    Please update it in the post not in the comment – mvg Dec 31 '14 at 12:49

2 Answers2

1

Take a look at organization value in Json. It's integer value but in Java object is Organization

Linh
  • 1,024
  • 2
  • 16
  • 28
  • So,what i need to pass there? Should i pass this json object { "city": "TEST", "country": "TEST", "latitude": "1", "longitude": "1", "name": "TEST", "postalCode": "362001", "street": "TEST", "organization": {"id" : 3 , "name" : "TEST"} } – Devloper Dec 31 '14 at 13:33
0

You can use ObjectMapper to convert your json data to corresponding Object by using the following snippet:

ObjectMapper mapper = new ObjectMapper();
Location location= mapper.readValue(jsonData, Location.class);
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59