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. python如何實現(xiàn)word批量轉(zhuǎn)HTML2. python excel和yaml文件的讀取封裝3. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)4. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊5. moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決6. 詳解Java實現(xiàn)拓撲排序算法7. 詳解docker pull 下來的鏡像都存到了哪里8. Docker鏡像管理常用操作代碼示例9. 關(guān)于 Android WebView 的內(nèi)存泄露問題10. Python中內(nèi)建模塊collections如何使用
