Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON feed to Java Object

I would like to know if there is a webpage/software that can "translate" a Json feed object to a Java object with attributes.

For example :

{
            'firstName': 'John',
            'lastName': 'Smith',
            'address': {
                'streetAddress': '21 2nd Street',
                'city': 'New York'
            }
        }

Would become:

class Person {
    private String firstName;
    private String lastName;
    private Address address;

    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public Address getAddress() { return address; }

    public void setFirstName(String firstName) { this.firstName = firstName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public void setAddress(Address address) { this.address = address; }

    public String toString() {
        return String.format("firstName: %s, lastName: %s, address: [%s]", firstName, lastName, address);
    }
}

class Address {
    private String streetAddress;
    private String city;

    public String getStreetAddress() { return streetAddress; }
    public String getCity() { return city; }

    public void setStreetAddress(String streetAddress) { this.streetAddress = streetAddress; }
    public void setCity(String city) { this.city = city; }

    public String toString() {
        return String.format("streetAddress: %s, city: %s", streetAddress, city);
    }
}

I'm not asking that because I'm lazy, but the JSON I would like to parse has quite a lot of attribute.

like image 210
Roch Avatar asked Mar 13 '10 10:03

Roch


People also ask

How do you convert JSON to Java object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

Can JSON be used with Java?

The Java API for JSON Processing provides portable APIs to parse, generate, transform, and query JSON. JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write.

What is JSON object in Java?

A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names.


1 Answers

I've successfully used json-lib for json serialization and deserialization. Your example would look like this:

String json = "{'firstName': 'John', 'lastName': 'Smith', 'address': {'streetAddress': '21 2nd Street', 'city': 'New York'}}";
JSONObject jsonObject = JSONObject.fromObject(json);
Person bean = (Person) JSONObject.toBean(jsonObject, Person.class);
System.out.println(bean);

And prints

firstName: John, lastName: Smith, address: [streetAddress: 21 2nd Street, city: New York]

If you need to customize it there are plenty of extension hooks. In my application i added support for serializing a Locale to a string "sv_SE" rather than an object. And for deserializing that same string to a Locale object.

like image 132
sigget Avatar answered Sep 22 '22 13:09

sigget