Java Json序列化反序列化示例
package json; import java.io.StringReader; import java.util.Date; import antlr.RecognitionException; import antlr.TokenStreamException; import com.sdicons.json.mapper.JSONMapper; import com.sdicons.json.mapper.MapperException; import com.sdicons.json.model.JSONValue; import com.sdicons.json.parser.JSONParser; public class JsonSerialize { /** * @param args * @throws MapperException * @throws RecognitionException * @throws TokenStreamException */ public static void main(String[] args) throws MapperException, TokenStreamException, RecognitionException { JSONValue jsonValue = JSONMapper.toJSON(new Date()); String jsonStr = jsonValue.render(true); System.out.println(jsonStr); JSONParser parser = new JSONParser(new StringReader(jsonStr)); Date date = (Date) JSONMapper.toJava(parser.nextValue(), Date.class); System.out.println(date); } }