Showing posts with label google-gson. Show all posts
Showing posts with label google-gson. Show all posts

Friday, July 25, 2014

Java: Convert Map/List to JSON Using google-gson


package com.pkm.com.java.json;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainClass {
    public static void main(String[] args) {
        Map dataMap = new HashMap();
        dataMap.put("args1", "Args 1");
        dataMap.put("args2", "Args 2");
        dataMap.put("args3", "Args 3");
        dataMap.put("args4", "Args 4");
        
        Map subMap = new HashMap();
        subMap.put("subArgs1", "Sub Args 1");
        subMap.put("subArgs2", "Sub Args 2");
        subMap.put("subArgs3", "Sub Args 3");
        dataMap.put("args5", subMap);
        
        List subList = new ArrayList();
        subList.add("List 1");
        subList.add("List 2");
        subList.add("List 3");
        subList.add(subMap);
        dataMap.put("args6", subList);
        
        Gson gson = new Gson();
        String json = gson.toJson(dataMap);
        System.out.println("json = " + json);
        
        Map dataMapFromJSONString = gson.fromJson(json, Map.class);
        display(dataMapFromJSONString, 0);
    }
    
    private static void display(Map map, Integer depth) {
        for (Object key : map.keySet()) {
            Object value = map.get(key.toString());
            if (value == null) {
                displaySpace(depth);
                System.out.println(key + ": <NULL>");
            }
            else if (value instanceof Map) {
                displaySpace(depth);
                System.out.println(key + ": ");
                Integer nextDepth = depth + 1;
                display((Map) value, nextDepth);
            }
            else if (value instanceof List) {
                displaySpace(depth);
                System.out.println(key + ": ");
                Integer nextDepth = depth + 1;
                display((List) value, nextDepth);
            }
            else {
                displaySpace(depth);
                System.out.println(key + ": " + value);
            }
        }
    }
    
    private static void display(List list, Integer depth) {
        for (Integer index = 0; index < list.size(); index++) {
            Object value = list.get(index);
            if (value == null) {
                displaySpace(depth);
                System.out.println("<NULL>");
            }
            else if (value instanceof Map) {
                displaySpace(depth);
                System.out.print("---->\n");
                Integer nextDepth = depth + 1;
                display((Map) value, nextDepth);
            }
            else if (value instanceof List) {
                displaySpace(depth);
                System.out.print("---->\n");
                Integer nextDepth = depth + 1;
                display((List) value, nextDepth);
            }
            else {
                displaySpace(depth);
                System.out.println(value);
            }
        }
    }
    
    private static void displaySpace(Integer depth) {
        for (Integer index = 0; index < depth; index++) {
            System.out.print("    ");
        }
    }
}

Google gson link
Download full source code

Output:


JSON String = {"args5":{"subArgs1":"Sub Args 1","subArgs2":"Sub Args 2","subArgs3":"Sub Args 3"},"args6":["List 1","List 2","List 3",{"subArgs1":"Sub Args 1","subArgs2":"Sub Args 2","subArgs3":"Sub Args 3"}],"args3":"Args 3","args4":"Args 4","args1":"Args 1","args2":"Args 2"}

Data Display From JSON String:

args5: 
    subArgs1: Sub Args 1
    subArgs2: Sub Args 2
    subArgs3: Sub Args 3
args6: 
    List 1
    List 2
    List 3
    ---->
        subArgs1: Sub Args 1
        subArgs2: Sub Args 2
        subArgs3: Sub Args 3
args3: Args 3
args4: Args 4
args1: Args 1
args2: Args 2