JAVA中JSONObject對象和Map對象之間的相互轉(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對象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對象:'+data.toString());
下面是輸出內(nèi)容:
{total=2, contend=[{'carid':'0','bid':'22'},{'carid':'0','bid':'22'}], result=100}
2.由Map對象轉(zhuǎn)換成json字符串//map對象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對象和Map對象之間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)JAVA JSONObject和Map相互轉(zhuǎn)換內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. 詳解CSS偽元素的妙用單標(biāo)簽之美3. PHP字符串前后字符或空格刪除方法介紹4. React優(yōu)雅的封裝SvgIcon組件示例5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式7. ASP基礎(chǔ)知識Command對象講解8. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲9. jsp+mysql實(shí)現(xiàn)網(wǎng)頁的分頁查詢10. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容
