首先,json转map

方法一:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Gson gson = new Gson();
 
String strjson  例如:
strjson  = { "ret":0, "msg":"", "nickname":"xxx", "figureurl":"http://qzapp.qlogo.cn/qzapp/100226195/C399C7B2880641627CED3EEF9DEB8E30/30", "figureurl_1":"http://qzapp.qlogo.cn/qzapp/100226195/C399C7B2880641627CED3EEF9DEB8E30/50", "figureurl_2":"http://qzapp.qlogo.cn/qzapp/100226195/C399C7B2880641627CED3EEF9DEB8E30/100", "gender":"xxx", "vip":"0", "level":"0", "is_yellow_year_vip":"0" }
 
Map infoMap = gson.fromJson(strjson, new TypeToken<Map<String, String>>(){}.getType());
 
方法二:
 

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互转的更多相关文章

  1. 转:JSON与Map互转

    JSON字符串与Map互转   //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return J ...

  2. FastJSON 简介及其Map/JSON/String 互转

    在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...

  3. JSON — Java与JSON数据互转

    转换时Bean所要求的: 被转换的Bean必需是public的. Bean被转换的属性一定要有对应的get方法,且一定要是public的. Bean中不能用引用自身的this的属性,否则运行时出现et ...

  4. DataTable 和Json 字符串互转

    #region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  5. bean 与 map 互转.

    package com.sprucetec.tms.distribute.utils;import java.beans.BeanInfo;import java.beans.Introspectio ...

  6. C#中另辟蹊径解决JSON / XML互转的问题

    C#中另辟蹊径解决JSON / XML互转的问题 最近在一个POC的项目中要用到JSON和XML的相互转换, 虽然我知道很多类库如JSON.NET具备这种功能, 但是我还是另辟蹊径的使用Spider ...

  7. 二:C#对象、集合、DataTable与Json内容互转示例;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  8. struct2json -- C结构体与 JSON 快速互转库V1.0发布

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/zhutianlong/article/d ...

  9. 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互转,其他的以后在补充.............. ...

  10. JavaScript Json与Map互转以及Map对象的取值方式

    Json格式(Json字符串) : var json='{"name": "lily","age":"15"}' Map ...

随机推荐

  1. Android 有用的快捷键

    The powerful Android Studio 08 Jun 2016 Android Studio is the official tool for Android development ...

  2. Python中yield深入理解

    众所周知,python中的yield有这样的用法: def test(alist): for i in alist: yield i 这样,这个test函数就变成了一个生成器,当每次调用的时候,就会自 ...

  3. 为什么开发人员对于PHP语言褒贬不一

    PHP 语言,作为服务器端开发的脚本语言,在网站开发方面非常有名.从1995年 Rasmus Lerdorf 创建之后,W3Techs 的调查显示在已知的服务端编程语言中,PHP 占了82%.其中不乏 ...

  4. Oracle监控指标

    1.数据文件或数据设备 参考:http://f.dataguru.cn/thread-106901-1-1.html2.数据库日志空间活或回滚段(包括大小.设备.文件及可用率.日志空间竞争情况或回滚段 ...

  5. redhat5安装jdk6、eclipse和tomcat6

    redhat5安装jdk6.eclipse和tomcat6 1.安装jdk6 首先下载jdk(jdk-6u13-linux-i586.bin),对于redhat5,本人强烈建议不要安装jdk7,因为有 ...

  6. #include <boost/unordered_set.hpp>

    boost.unordered在C++标准容器std::set,std::multiset,std::map和std::multimap的基础上多实现了四个容器:boost::unordered_se ...

  7. Exchange Cards(dfs)

    Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also f ...

  8. SHUTDOWN: Active processes prevent shutdown operation

    在使用shutdown immediate关闭数据库时hang住,查看alert 日志,遭遇了SHUTDOWN: Active processes prevent shutdown operation ...

  9. jQuery 遍历 - siblings() 方法

    本文来自:http://www.w3school.com.cn/jquery/traversing_siblings.asp jQuery 遍历参考手册 实例 查找每个 p 元素的所有类名为 &quo ...

  10. poj 2411 Mondriaan&#39;s Dream 【dp】

    题目:id=2411" target="_blank">poj 2411 Mondriaan's Dream 题意:给出一个n*m的矩阵,让你用1*2的矩阵铺满,然 ...