http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_a_JSON_object
javac -cp ./json-simple.jar SplitDemo.java
java -cp _CLASSPATH:./json-simple.jar SplitDemo
public class QueryAttribute implements JSONStreamAware { private String query; private String cat; private int guanwang; private String icon; private String entity_name; private double score; private String clkurl; public QueryAttribute(String query, double score, String cat, String entity_name, int guanwang, String icon, String clkurl) { this.query = query; this.cat = cat; this.entity_name = entity_name; this.score = score; this.guanwang = guanwang; this.icon = icon; this.clkurl = clkurl; } public void writeJSONString(Writer out) throws IOException { LinkedHashMap obj = new LinkedHashMap(); obj.put("query", query); obj.put("score", score); obj.put("cat", cat); obj.put("ename", entity_name); obj.put("gw", guanwang); obj.put("icon", icon); obj.put("clkurl", clkurl); JSONValue.writeJSONString(obj, out); } }
parser
import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.JSONValue; import java.util.*; public class Segment { public static void main(String[] args) { String s="[0,{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}]"; Object obj = JSONValue.parse(s); JSONArray array = (JSONArray)obj; System.out.println(array.get(0)); System.out.println(array.get(1)); System.out.println(); JSONObject obj2 = (JSONObject)array.get(1); System.out.println("=============field "1"======="); System.out.println(obj2.get("1")); System.out.println(obj); s = "{}"; obj = JSONValue.parse(s); System.out.println(obj); s = "[5,]"; obj = JSONValue.parse(s); System.out.println(obj); s = "[5,,2]"; obj = JSONValue.parse(s); System.out.println(obj); } }