赞
踩
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
Maven
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
User u =new User();
u.setId(521);
u.setUName("忘忧");
u.setUPwd("88888888");
String json = JSON.toJSONString(user); //序列化
JSONObject jsonObject=(JSONObject)JSON.toJSON(user);
jsonObject.getIntValue("id")+jsonObject.getString("uPwd")
User user =JSON.parseObject(json,User.class); //反序列化
User user=JSON.toJavaObject(jsonObject, User.class);
{ "msg": "操作成功!", "code": "200", "data": [ { "id": "0683d34aa31e4323be6d21b6d2bbd19eE41891461", "status": 1, "netAddress": "36.7.108.200" }, { "id": "0683d34aa31e4323be6d21b6d2bbd19eE41891466", "status": 1, "netAddress": "39.7.109.208" } ], }
Map<String,Object> maps = JSONObject.parseObject(json2,Map.class);
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(maps));
[
{
"id": "0683d34aa31e4323be6d21b6d2bbd19eE41891461",
"status": 1,
"netAddress": "36.7.108.200"
},
{
"id": "0683d34aa31e4323be6d21b6d2bbd19eE41891466",
"status": 1,
"netAddress": "39.7.109.208"
}
]
JSONArray json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的 Json对象中添加的是键值对,JSONArray中添加的是Json对象
parseArry用于如果是用[]包裹的 parseObject用于如果是{}包裹的补充
List<Map> listMaps = JSONArray.parseArray(JSON.toJSONString(data),Map.class);
List<Map> mapsList = JSONObject.parseArray(JSON.toJSONString(data), Map.class);
String jsonString=JSON.toJSONString(jsonObject);
JSONObject jsonObject=JSON.parseObject(jsonString);
jsonObject.getString("uName");
JSONArray jArray=JSON.parseArray(JSON.toJSONString(userList));
String json= "[ { "id": "0683d34aa31e4323be6d21b6d2bbd19eE41891461", "status": 1, "netAddress": "36.7.108.200" }, { "id": "0683d34aa31e4323be6d21b6d2bbd19eE41891466", "status": 1, "netAddress": "39.7.109.208" } ]" List<body> personList = JSON.parseObject(json, new TypeReference<List<body>>(){});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。