JAVA中JSONObject對(duì)象和Map對(duì)象之間的相互轉(zhuǎn)換
如json字符串:{'contend':[{'bid':'22','carid':'0'},{'bid':'22','carid':'0'}],'result':100,'total':2}
下面直接附代碼:
//json字符串String jsondata='{'contend':[{'bid':'22','carid':'0'},{'bid':'22','carid':'0'}],'result':100,'total':2}';JSONObject obj= JSON.parseObject(jsondata);//map對(duì)象Map<String, Object> data =new HashMap<>();//循環(huán)轉(zhuǎn)換 Iterator it =obj.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = (Entry<String, Object>) it.next(); data.put(entry.getKey(), entry.getValue()); }System.out.println('map對(duì)象:'+data.toString());
下面是輸出內(nèi)容:
{total=2, contend=[{'carid':'0','bid':'22'},{'carid':'0','bid':'22'}], result=100}
2.由Map對(duì)象轉(zhuǎn)換成json字符串//map對(duì)象Map<String, Object> data =new HashMap<>();String x =JSONObject.toJSONString(data);System.out.println('json字符串:'+x);
下面是輸出內(nèi)容:
{'total':2,'result':100,'contend':[{'carid':'0','bid':'22'},{'carid':'0','bid':'22'}]}
到此這篇關(guān)于JAVA中JSONObject對(duì)象和Map對(duì)象之間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)JAVA JSONObject和Map相互轉(zhuǎn)換內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python web框架的總結(jié)2. Python如何進(jìn)行時(shí)間處理3. 詳解Python模塊化編程與裝飾器4. python使用ctypes庫調(diào)用DLL動(dòng)態(tài)鏈接庫5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式7. python logging 重復(fù)寫日志問題解決辦法詳解8. Python中l(wèi)ogger日志模塊詳解9. python裝飾器三種裝飾模式的簡單分析10. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析
