Archives: JSON Example

Java – Convert Map to JSON

[code lang=”java”]import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class MapToJson {

public static void main(String[] args) {
Map<String, String> colours = new HashMap<String, String>();
colours.put("BLACK", "#000000");
colours.put("RED", "#FF0000");
colours.put("GREEN", "#008000");
colours.put("BLUE", "#0000FF");
colours.put("YELLOW", "#FFFF00");
colours.put("WHITE", "#FFFFFF");

//
// Convert a Map into JSON string.
//
Gson gson = new Gson();
String json = gson.toJson(colours);
System.out.println("json = " + json);

//
// Convert JSON string back to Map.
//
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map = gson.fromJson(json, type);
for (String key : map.keySet()) {
System.out.println("map.get = " + map.get(key));
}
}

}[/code]

Output:

json = {“WHITE”:”#FFFFFF”,”BLUE”:”#0000FF”,”YELLOW”:”#FFFF00″,”GREEN”:”#008000″,”BLACK”:”#000000″,”RED”:”#FF0000″}
map.get = #FFFFFF
map.get = #0000FF
map.get = #FFFF00
map.get = #008000
map.get = #000000
map.get = #FF0000


Java – Read JSON Data Simple Example

In below example, it uses JSONParser to read and display JSON data from a file named “test.json“ using JSON.simple, a simple Java library for JSON processing.
[code lang=”java”]
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleExample {
public static void main(String[] args) {

JSONParser parser = new JSONParser();

try {

Object obj = parser.parse(new FileReader("c:\\test.json"));

JSONObject jsonObject = (JSONObject) obj;

String name = (String) jsonObject.get("name");
System.out.println(name);

long age = (Long) jsonObject.get("age");
System.out.println(age);

// loop array
JSONArray msg = (JSONArray) jsonObject.get("messages");
Iterator&lt;String&gt; iterator = msg.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}

}

}
[/code]
Output:

prasanna
25
msg 1
msg 2
msg 3


Java – Write JSON Data Simple Example

In below example, it writes JSON data via JSONObject and JSONArray, and save it into a file named “test.json“ using JSON.simple, a simple Java library for JSON processing.
[code lang=”java”]import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonSimpleExample {
public static void main(String[] args) {

JSONObject obj = new JSONObject();
obj.put("name", "prasanna");
obj.put("age", new Integer(25));

JSONArray list = new JSONArray();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");

obj.put("messages", list);

try {

FileWriter file = new FileWriter("c:\\test.json");
file.write(obj.toJSONString());
file.flush();
file.close();

} catch (IOException e) {
e.printStackTrace();
}

System.out.print(obj);

}

}[/code]
Output: (see content of file “test.json”)

{
“age”:25,
“name”:”prasanna”,
“messages”:[“msg 1″,”msg 2″,”msg 3”]
}