<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> |
|
來(lái)自: 阿修羅之獅猿授 > 《unity數(shù)據(jù)序列化》