首先,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. 括号配对问题--nyoj-2(栈)

    括号配对问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0<N<=10 ...

  2. Ubuntu安装与初始配置

    转载请注明作者:梦里风林 更多文章查看我的个人站: ahangchen.site 适用于Ubuntu版本 14.04/16.04LTS 64位 先上图 双系统安装 划分空闲磁盘,U盘安装ubuntu ...

  3. QT下的几种透明效果(三种方法:调色板,透明度属性,自绘)

    1.窗口整体透明,但是窗体上的控件不透明.    通过设置窗体的背景色来实现,将背景色设置为全透.  QPalette pal = palette();  pal.setColor(QPalette: ...

  4. asp.net中,我们使用ashx获取数据列表,在前端使用$.ajax()解析

    一直在想在asp.net中怎么才能向在java中那样用struts那样做页面请求. 当然asp.net mvc就是类似struts的东西吧,不过还没来得及学习. 今天就用ashx来接收页面请求,并调用 ...

  5. &lt;转&gt;凯文&#183;凯利斯坦福演讲-预言未来20年科技潮流

    Note:未来全部的生意都是关于数据的生意,近场通信.自组网介入网络.人工智能...,如今的IT科技界是否仅仅是冰山一角.斑斓舞台帷幕的一丝缝隙? 原文出处: 中欧管理工商学院   欢迎分享原创到伯乐 ...

  6. ORACLE表空间bigfile和smallfile

    BIGFILE | SMALLFILE Use this clause to determine whether the tablespace is a bigfile or smallfile ta ...

  7. 【最大点权独立集】【HDU1565】【方格取数】

    题目大意: 给你一个n*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大. 初看: 没想法 ...

  8. zTouch-移动端触屏开发利器(zepto touch扩展)

    * Zepto.js v1.0.1 touch extend (Zepto.js v1.0.1 的swipe touch扩展)js-处理手机移动端web触屏手势动作. Zepto.js v1.0.1版 ...

  9. UVA1600 Patrol Robot

    题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...

  10. HasMap

    您还未登录 ! 登录 注册 论坛首页 → Java企业应用论坛 → 深入理解HashMap 全部 Hibernate Spring Struts iBATIS 企业应用 Lucene SOA Java ...