分享

JSON的序列化和反序列化

 阿修罗之狮猿授 2016-07-08
<textarea cols="50" rows="15" name="code" class="Java">import java.io.StringReader; import test.vo.ClientTicketOrder; import test.vo.TicketItem; 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 TestJsonTools { /** * @param args * @throws MapperException * @throws RecognitionException * @throws TokenStreamException */ public static void main(String[] args) throws MapperException, TokenStreamException, RecognitionException { ClientTicketOrder order = new ClientTicketOrder(); order.setClientOrderNo("0812333"); TicketItem tItem = new TicketItem(); tItem.setPnr("UY8YG"); order.setTicketItems(new TicketItem[]{tItem}); //JavaBean to JSON JSONValue jsonValue = JSONMapper.toJSON( order ); String jsonStr = jsonValue.render(false); System.out.println(jsonStr); //JSON to JavaBean JSONParser parser = new JSONParser(new StringReader(jsonStr)); ClientTicketOrder u = (ClientTicketOrder) JSONMapper.toJava(parser.nextValue(), ClientTicketOrder.class); System.out.println(u.getClientOrderNo() + " " + u.getTicketItems()[0].getPnr()); } } </textarea>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约