Using java, I need to parse a json text file that will contain data according to the layout shown below. The number of email addresses per store will vary. The point is to get a list of stores & corresponding email addresses, then send emails to the addresses. I have a little experience with java but none with json. Can anyone recommend a good way to do this, hopefully with some examples? Thanks!

{
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>","<email address2>"]}
    ],
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>","<email address2>","<email address3>"]}
    ],
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>"]}
    ],
}

That's a valid but logically incorrect JSON representation of the data. Given that JSON objects are "string: value" pairs, having the same "string key" for all items will result in only one item being returned when you try to get all "STORE" elements.

For e.g. try pasting the following JSON in this validator and press validate: http://jsonlint.com/

{
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>","<email address2>"]}
    ],
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>","<email address2>","<email address3>"]}
    ],
    "STORE": [
        {"storeName":"<store name>"},
        {"emailAddresses": ["<email address1>"]}
    ]
}

As you can see, only the last element showed up. A better representation would be an array of stores keyed by "stores"; something like:

{
    "stores": [
        {
            "storeName": "<store name>",
            "emailAddresses": [
                "<email address1>",
                "<email address2>"
            ]
        },
        {
            "storeName": "<store name>",
            "emailAddresses": [
                "<email address1>",
                "<email address2>",
                "<email address3>"
            ]
        },
        {
            "storeName": "<store name>",
            "emailAddresses": [
                "<email address1>"
            ]
        }
    ]
}

As far as reading the JSON file is concerned, the simplest way I know of is to use the data binding offered by the Jackson JSON library. Download the library from: http://wiki.fasterxml.com/JacksonDownload under the Download 2.x section (all 3 jars).

To use the auto binding functionality, we need to create classes corresponding to the Stores and Store. Something like:

public class Stores {        
    private List<Store> stores;    
    public List<Store> getStores() {
        return stores;
    }    
    public void setStores(List<Store> stores) {
        this.stores = stores;
    }    
}



public class Store {        
    private String storeName;        
    private String[] emailAddresses;    
    public String getStoreName() {
        return storeName;
    }    
    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }    
    public String[] getEmailAddresses() {
        return emailAddresses;
    }    
    public void setEmailAddresses(String[] emailAddresses) {
        this.emailAddresses = emailAddresses;
    }    
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Store [storeName=").append(storeName).append(", emailAddresses=")
                .append(Arrays.toString(emailAddresses)).append("]");
        return builder.toString();
    }
}

And now the main class which will use these POJO's:

public class JsonTest {

    public static void main(final String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // you can even pass a File object to the readValue method
        Stores myStores = mapper.readValue(JsonTest.class.getResourceAsStream("test.json"), Stores.class);
        for (final Store store : myStores.getStores()) {
            System.out.println(store);
        }
    }

}

And we are done! After running you should get something like:

Store [storeName=<store name 1>, emailAddresses=[<email address1>, <email address2>]]
Store [storeName=<store name 2>, emailAddresses=[<email address1>, <email address2>, <email address3>]]
Store [storeName=<store name 3>, emailAddresses=[<email address1>]]

Now you can use the Store objects just like usual Java objects in your code to get the store name, email etc.

I have a json format objects. Actually i need to store this objects in to database. i understood your tutoial. but i want to know where i need to store test.json? please send me the step by step tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.