zoukankan      html  css  js  c++  java
  • json、map互转

    首先,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 = 0; 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
  • 相关阅读:
    局部特征点检测 (Local Point Detector)
    算法
    64位编程
    QT开发之mock原理
    C#实现全角字符和半角字符转换
    QTableWidget基本功能总结(转)
    非const引用不能绑定非左值(nolvalue) .
    QTableWidget 应用总结
    QString和string类型相互转换(转)
    XML解析中文字符
  • 原文地址:https://www.cnblogs.com/ws5861/p/4253530.html
Copyright © 2011-2022 走看看