2 ak46143279 ak46143279 于 2016.05.10 15:51 提问

新手求教,字符串转换成JSON

ActiveMQ消息队列:发布者代码:
messagePublisher = (SimulatedMarketMessagePublisher)applicationContext.getBean("messagePublisher");
Message message = new Message();
Head head = new Head();
SimTradeRecord str = new SimTradeRecord();
head.setMsgId(1);
head.setCode(200);
head.setServerTime(1461904735);
head.setMessage("操作成功");
message.setHead(head);
str.setSignalId(100081380);
str.setAction("买入开仓");
str.setContract("IF000");
str.setInstrument("期货");
str.setQuant(1);
str.setStategyId(72);
str.setSymbol("IF");
str.setTradedTime(new Date());
str.setTradePrice(new BigDecimal("3238.8"));
str.setTradeRecordId(1555);
message.setSimTradeRecord(str);
messagePublisher.sendStrategyTradeMessage(message.toString());
这个字符串已经传到订阅者监听中:
Message [simTradeRecord=SimTradeRecord [id=0, tradeRecordId=1555, symbol=IF, contract=IF000, signalId=100081380, stategyId=72, action=买入开仓, quant=1, instrument=期货, tradePrice=3238.8, tradedTime=Tue May 10 15:44:00 CST 2016], head=Head [msgId=1, code=200, serverTime=1461904735, message=操作成功]]

我要怎么把他解析成这样的json格式
{"head":{"action":"买入开仓","contract":"IF000","id":2,"instrument":"期货","quant":1,"signalId":100081379,"stategyId":72,"symbol":"IF","tradePrice":3238.8000000000001818989403545856475830078125,"tradeRecordId":12333,"tradedTime":{"date":1,"day":4,"hours":8,"minutes":0,"month":0,"seconds":2,"time":2007,"timezoneOffset":-480,"year":70}},"body":{"code":200,"message":"操作成功","msgId":1,"serverTime":1461904735}}

谢谢

4个回答

zhaihonghonghzh
zhaihonghonghzh   2016.05.10 15:59

根据key拿值并进行相应的处理,然后存入map中就行了

CSDNXIAON
CSDNXIAON   2016.05.10 16:04

字符串转换成json
关于转换成json字符串的误区
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

u012377333
u012377333   Rxr 2016.05.10 16:26

你首先得解析这个消息,将这个消息解析成map,然后将map直接转化为json格式。
关键还是在于怎么解析这个消息。

oyljerry
oyljerry   Ds   Rxr 2016.05.10 16:27
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!