json、map互转
首先,json转map
方法一:
JSONObject jsonObject = new JSONObject(s);
Map<String,Object> map= new HashMap<String,Object>();
Iterator it = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (it.hasNext())
{
String key = String.valueOf(it.next());
String value = (String) jsonObject.get(key);
map.put(key, value);
}
return map;
方法三:
JSONObject jasonObject = JSONObject.fromObject(str);
Map<String, Object> map2 = (Map) jasonObject;
但此方法可能不适用, 百度的,出过问题,做个记录。
map转json
JSONObject jsonObject = JSONObject.fromMap(map);
关于jsonArray,留个示例:
public CarDataResultList<CarIntroEntity> parserJson(String paramString) throws Car273Exception {
try {
JSONObject jsonObj = new JSONObject(paramString);
if (jsonObj.has("total")) {
defCarIntroList.total = jsonObj.getInt("total");
}
if (jsonObj.has("info")) {
JSONArray infoArray = jsonObj.getJSONArray("info");
for (int i = ; i < infoArray.length(); i++) {
CarIntroEntity carIntroEntity = new CarIntroEntity();
JSONObject json = infoArray.getJSONObject(i);
if (json.has("id")) {
carIntroEntity.setId(json.getString("id"));
}
.
.
.
defCarIntroList.infoList.add(carIntroEntity);
}
}
} catch (JSONException e) {
throw ce;
}
paramString 的值如下,可做参考:
paramString ={"total":6318,"fix_num":"400-0018-273","ext":{"brand_id":0,"brand_name":"","series_id":0,"series_name":""},"info":[{"tags":["ckb","mdyyc","dk","xsz"],"seller_name":"先生","ext_phone":"915109","cover_photo":"eqsdfs01\/M01\/2B\/2F\/CgDJBFQFcpCAGeGcAAGYc3bpTCs196.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":1,"mark_type":"1","ip":"0","id":"13787496","title":"雪佛兰科鲁兹 2013款 手动1.6","follow_user_name":"游鑫","price":"80000.0","update_time":"1410843893","tag_title":"","create_time":"1409643771","kilometer":"25000","telephone":"*****","card_time":"1370016000"},{"tags":["ckb","dk"],"seller_name":"姚小姐","ext_phone":"559247","cover_photo":"eqsdfs01\/M03\/1F\/84\/CgDJBVP6liOABlHrAADf3YoibBs667.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":0,"mark_type":"0","ip":"0","id":"13616672","title":"本田思域 2009款 自动1.8","follow_user_name":"邓燕清","price":"99000.0","update_time":"1410845158","tag_title":"","create_time":"1408930070","kilometer":"24000","telephone":"*****","card_time":"1349020800"},{"tags":["ckb","xsz"],"seller_name":"先生","ext_phone":"176772","cover_photo":"eqsdfs01\/M05\/32\/A1\/CgDJBVQRDt2AfZ3aACG9G6Q7GZY419.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":1,"mark_type":"1","ip":"0","id":"13964192","title":"奇瑞A3 2009款 手动1.6","follow_user_name":"葛嘉辉","price":"35000.0","update_time":"1410836810","tag_title":"","create_time":"1410404394","kilometer":"39000","telephone":"*****","card_time":"1251734400"},{"tags":["ckb","mdyyc"],"seller_name":"先生","ext_phone":"236151","cover_photo":"eqsdfs01\/M03\/37\/63\/CgDJBFQSyJOAb1pYAAIGQrlS3CM435.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":0,"mark_type":"0","ip":"0","id":"13993375","title":"雪佛兰乐骋 2008款 手动1.4","follow_user_name":"葛嘉辉","price":"35000.0","update_time":"1410836804","tag_title":"","create_time":"1410517436","kilometer":"81000","telephone":"*****","card_time":"1225468800"},{"tags":["ckb","mdyyc"],"seller_name":"先生","ext_phone":"683248","cover_photo":"eqsdfs01\/M03\/24\/6F\/CgDJBFP8gwKAGcioAAs3Tj9JKOk286.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":0,"mark_type":"0","ip":"0","id":"13649084","title":"别克凯越 2008款 1.6 手动 舒适版","follow_user_name":"连盛隆","price":"43000.0","update_time":"1410836595","tag_title":"","create_time":"1409058393","kilometer":"100000","telephone":"*****","card_time":"1206979200"},{"tags":["ckb","mdyyc"],"seller_name":"C先生","ext_phone":"838970","cover_photo":"eqsdfs01\/M03\/2E\/28\/CgDJBFQJcDiAN-aZAAESmQLuSSA706.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":0,"mark_type":"0","ip":"0","id":"13850735","title":"丰田威驰 2007款 自动1.3","follow_user_name":"曹奇铨","price":"48000.0","update_time":"1410833707","tag_title":"","create_time":"1409904727","kilometer":"59000","telephone":"*****","card_time":"1188576000"},{"tags":["ckb","mdyyc","xsz"],"seller_name":"陈先生","ext_phone":"115706","cover_photo":"eqsdfs01\/M05\/1F\/77\/CgDJBVP6kGaATGZfAALl-OgCQz4848.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":1,"mark_type":"1","ip":"0","id":"13408591","title":"本田雅阁 2003年款 雅阁 3.0 自动 Vtec 豪华版","follow_user_name":"余浩军","price":"55000.0","update_time":"1410833642","tag_title":"","create_time":"1408011706","kilometer":"180000","telephone":"*****","card_time":"1049126400"},{"tags":["ckb","mdyyc","xsz"],"seller_name":"先生","ext_phone":"793890","cover_photo":"eqsdfs01\/M05\/19\/63\/CgDJBVPzKaqAF4mlAAtmBEgvnLw524.jpg","plate_city_name":"福州","advisor_status":0,"driving_status":1,"mark_type":"1","ip":"0","id":"13420413","title":"福特蒙迪欧致胜 2010款 2.3 手自一体 豪华运动型导航版","follow_user_name":"洪城","price":"98000.0","update_time":"1410831941","tag_title":"","create_time":"1408067058","kilometer":"73000","telephone":"*****","card_time":"1249056000"},{"tags":["ckb","mdyyc","xsz"],"seller_n

json、map互转的更多相关文章
- 转:JSON与Map互转
JSON字符串与Map互转 //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return J ...
- FastJSON 简介及其Map/JSON/String 互转
在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...
- JSON — Java与JSON数据互转
转换时Bean所要求的: 被转换的Bean必需是public的. Bean被转换的属性一定要有对应的get方法,且一定要是public的. Bean中不能用引用自身的this的属性,否则运行时出现et ...
- DataTable 和Json 字符串互转
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- bean 与 map 互转.
package com.sprucetec.tms.distribute.utils;import java.beans.BeanInfo;import java.beans.Introspectio ...
- C#中另辟蹊径解决JSON / XML互转的问题
C#中另辟蹊径解决JSON / XML互转的问题 最近在一个POC的项目中要用到JSON和XML的相互转换, 虽然我知道很多类库如JSON.NET具备这种功能, 但是我还是另辟蹊径的使用Spider ...
- 二:C#对象、集合、DataTable与Json内容互转示例;
导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型: 二:C#对象.集合.DataTable与Json内容互转示例: ...
- struct2json -- C结构体与 JSON 快速互转库V1.0发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/zhutianlong/article/d ...
- Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)
目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充.............. ...
- JavaScript Json与Map互转以及Map对象的取值方式
Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...
随机推荐
- PC远程调试移动设备(转载)
我们在移动端进行前端开发时,会遇到一个让人头痛但不得不面对的问题--调试. 在 PC 机器上,我们有功能强大的 Chrome DevTools.Firebug,即便是老版本的 IE ,我们也可以安装微 ...
- 代码设置layout_weight attribute
代码设置 LinearLayout权重比例之小结: 如果在LinearLayout添加子View,那么只有一个View的时候设置所占的比例一定要设置LinearLayout总weightsum.不然会 ...
- wxwidgets demo
环境说明: IOS: mac 10.9 wx: wxWidgets-2.9.5 (http://www.wxwidgets.org/downloads/) gcc: $gcci686-apple- ...
- 在Win8上安装pyinstaller打包python成为可执行文件
首先我使用的电脑系统是: Windows-8-6.2.9200 Python的版本是: 2.7.8 默认已安装python2.7且设置好了环境变量. 仅为个人记录,非教程. 首先先安装pip: 首先先 ...
- AdminCMD
program AdminCMD; {$APPTYPE CONSOLE} uses Windows, ShellApi, SysUtils; function RunAsAdmin(const iEx ...
- IE9 "CSS 因 Mime 类型不匹配而被忽略“问题
写页面的时候在chrome,fireforks等页面上显示正常,但是换成IE9之后就完全没有样式了.IE真是个奇葩的怪胎.它的报错信息是’CSS 因 Mime 类型不匹配而被忽略‘,也就是说所有的.c ...
- 获取ActiveX控件本身所在的路径 和 error PRJ0050
一. CString GetCurPath() { TCHAR exeFullPath[MAX_PATH]; CString strPath; ...
- boost::asio 使用 libcurl
curl 使用 asio 的官方样例 http://curl.haxx.se/libcurl/c/asiohiper.html, 但这个例子用起来有很明细的 bug,asio 异步IO 只注册一次,也 ...
- poj1163 dp入门
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36811 Accepted: 22048 De ...
- Valentino发布2013秋冬高级定制系列_菁华时尚生活,时尚生活电子杂志,Fine Born China
Valentino发布2013秋冬高级定制系列_菁华时尚生活,时尚生活电子杂志,Fine Born China alentino发布2013秋冬高级定制系列 编辑: Mavis 图源于网络 图源于网络 ...