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'}]}
到此這篇關于JAVA中JSONObject對象和Map對象之間的相互轉(zhuǎn)換的文章就介紹到這了,更多相關JAVA JSONObject和Map相互轉(zhuǎn)換內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. 怎樣打開XML文件?xml文件如何打開?2. ASP.NET MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面3. Python如何解決secure_filename對中文不支持問題4. 不使用XMLHttpRequest對象實現(xiàn)Ajax效果的方法小結(jié)5. ASP.NET MVC限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)6. Python使用oslo.vmware管理ESXI虛擬機的示例參考7. ASP基礎入門第二篇(ASP基礎知識)8. ThinkPHP6使用JWT+中間件實現(xiàn)Token驗證實例詳解9. TP5使用RabbitMQ實現(xiàn)消息隊列的項目實踐10. JSP出現(xiàn)中文亂碼問題解決方法詳解
