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;
        }

时间: 2024-07-30 12:28:20

json、map互转的相关文章

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

在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个语言中 解析 json 的库都一大片(具体 json 格式与三方库的介绍请见:http://www.json.org/json-zh.html),比如 python 都集成在了内置库中,成为标准 API,今天我们要聊的是 java 中如何方便的使用 json 格式. 从上面的链接介绍中我们可以看到,

JAVA中,JSON MAP LIST的相互转换

1 JSON包含对象和数组,对应于JAVA中的JSONObject,JSONArray 2 String 转JSON对象 JSONObject.fromObject("String"); String 转JSON数组 JSONArray.fromObject("String"); 3 List 和 JSON互转 JSONObject.toBean() JSONArray.fromObject(List) JAVA中,JSON MAP LIST的相互转换,布布扣,bu

C# 调用API接口处理公共类 自带JSON实体互转类

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using System.Web; n

JSon SuperObject 研究2:数据集与JSON对象互转

JSon SuperObject 研究2:数据集与JSON对象互转 JSON不能完全替代XML,但绝对是未来的大势所趋,其优点是简单.体积小.解析更快.解析占用资源更少.在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂.下面的程序,或许你有不同的想法,如果你的想法更好更快,欢迎一起讨

bean 与 map 互转.

package com.sprucetec.tms.distribute.utils; import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.re

使用FastJSON,将对象或数组和JSON串互转

Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库.其开源的下载网址为:https://github.com/AlibabaTech/fastjson. 示例代码如下: [java] view plaincopy package test; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSON; class User { private String 

DataTable 和Json 字符串互转

#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> public class GetClassTypeJosn : IHttpHandler { /// <summary> /// 文件名:DataTable 和Json 字符串互转/// </summary> //用法说明实例 public void ProcessRequest(H

Delphi中JSon SuperObject 使用:数据集与JSON对象互转

在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂. 数据集字段信息,是一个完整的字典信息.因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息.我们设置其JSON信息如下: COLS:[字段列表信息],如: "Cols":[{"JsonType&quo

java对象与json串互转

1:java对象与json串转换: java对象—json串: JSONObject JSONStr = JSONObject.fromObject(object); String str = JSONStr.toString(); json串—java对象: JSONObject jsonObject = JSONObject.fromObject( jsonString ); Object pojo = JSONObject.toBean(jsonObject,pojoCalss); 2:j