Sunday 31 May 2015

Creating and parsing JSON using Json-simple



1. Simple JSON string creation.

Requirement: {"NAME":"USER","AGE":25,"COUNTRY":"INDIA"}

    public static void main(String[] args) {
        JSONObject createUserJson = new JSONObject();
        createUserJson.put("NAME", "GOPAL");
        createUserJson.put("AGE", 25);
        createUserJson.put("COUNTRY", "INDIA");
       
        String jsonString =  JSONValue.toJSONString(createUserJson);
        System.out.println(jsonString); 
      
    }

2. JSON creation with Array

Requirement:  
{"NAME":"USER","AGE":25,"COUNTRY":"INDIA",
    "ADDRESS" : [ {"STREET":"xyz", "POSTAL CODE":"654321","CITY":"BANGALORE"} ,
                               {"STREET":"abc", "POSTAL CODE":"987654","CITY":"BANGALORE"}
                             ]                              
}


public static void main(String[] args) {
        JSONObject createUserJson = new JSONObject();
        createUserJson.put("NAME", "GOPAL");
        createUserJson.put("AGE", 25);
        createUserJson.put("COUNTRY", "INDIA");
        

        List<Map> jsonArray = new ArrayList<Map>();
        Map<String, String> streetXYZ = new HashMap<String, String>();
        streetXYZ.put("STREET", "xyz");
        streetXYZ.put("POSTAL CODE", "654321");
        streetXYZ.put("CITY", "BANGALORE");

        Map<String, String> streetABC = new HashMap<String, String>();
        streetABC.put("STREET", "xyz");
        streetABC.put("POSTAL CODE", "654321");
        streetABC.put("CITY", "BANGALORE");

        jsonArray.add(streetXYZ);
        jsonArray.add(streetABC);

        createUserJson.put("ADDRESS", jsonArray);
 

        String jsonString =  JSONValue.toJSONString(createUserJson);
        System.out.println(jsonString); 
      
    }

3. Parsing JSON String - Example is parsing above JSON created.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

private static void parsingJsonString(String jsonString) {
        JSONParser parser = new JSONParser();
        try {
            JSONObject jsonObject = (JSONObject)parser.parse(jsonString);
            String name = (String)jsonObject.get("NAME");
            System.out.println(name);
           
            JSONArray addressArray = (JSONArray)jsonObject.get("ADDRESS");
            for (int i = 0; i < addressArray.size(); i++) {
                Object arrayObj = parser.parse(addressArray.get(i).toString());
                JSONObject jsonArrayObject = (JSONObject)arrayObj;
                System.out.println("Street " + jsonArrayObject.get("STREET"));
                System.out.println("Postal Code " +
                                   jsonArrayObject.get("POSTAL CODE"));
                System.out.println("City " + jsonArrayObject.get("CITY"));
                System.out.println("-------------------------------------");
            }

        } catch (ParseException e) {
            e.getMessage();
        }
  
}

*Using Json-simple-1.1.1.jar

*Comments and feedback are most welcomed, Thanks.

No comments:

Post a Comment