jackson、fastjson、net

1.JsonNode

package shangbo.jackson.demo19;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class App {
	public static void main(String[] args) throws Exception {
		// 实例化 ObjectMapper 对象
		ObjectMapper objectMapper = new ObjectMapper();

		// json 消息
		String json = "{\"firstname\":\"Bo\",\"lastname\":\"Shang\",\"age\":30}";
		
		// 将 json 转成 JsonNode 对象
		JsonNode rootNode = objectMapper.readTree(json);
		
		// 得到节点值
		JsonNode firstNameNode = rootNode.get("firstname");
		System.out.println("firstname:" + firstNameNode.asText());
		
		JsonNode ageNode = rootNode.get("age");
		System.out.println("age:" + ageNode.asInt());
		
		// 创建新节点
		ObjectNode newNode = objectMapper.createObjectNode();
		newNode.setAll((ObjectNode)rootNode);
		newNode.put("hometown", "dalian");
		
		// 将 JsonNode 对象转成 json
		String newjson = objectMapper.writeValueAsString(newNode);
		System.out.println(newjson);
	}
}

2.jackson objectMapper json字符串、对象bean、map、数组list互相转换

1.对象转json字符串
User user=new User();
String userJson=mapper.writeValueAsString(user);

2.Map转json字符串
Map map=new HashMap();  
String json=mapper.writeValueAsString(map);

3.数组list转json字符串
Student[] stuArr = {student1, student3};  
String jsonfromArr =  mapper.writeValueAsString(stuArr);

4.json字符串转对象
String expected = "{\"name\":\"Test\"}";
User user = mapper.readValue(expected, User.class);

5.json字符串转Map
String expected = "{\"name\":\"Test\"}";
Map userMap = mapper.readValue(expected, Map.class);

6.json字符串转对象数组List<object>
String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class);
List<User> userList = mapper.readValue(expected, listType);

7.json字符串转Map数组List<Map<String,Object>>
String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class);
List<Map<String,Object>> userMapList = mapper.readValue(expected, listType);

8.jackson默认将对象转换为LinkedHashMap:
String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
ArrayList arrayList = mapper.readValue(expected, ArrayList.class);

9.json字符串与list或map互转的方法
ObjectMapper objectMapper = new ObjectMapper();
//遇到date按照这种格式转换
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(fmt);
  String preference = "{name:'侯勇'}";
  //json字符串转map
  Map<String, String> preferenceMap = new HashMap<String, String>();
  preferenceMap = objectMapper.readValue(preference, preferenceMap.getClass());
  //map转json字符串
  String result=objectMapper.writeValueAsString(preferenceMap);

10.bean转换为map
List<Map<String,String>> returnList=new ArrayList<Map<String,String>>();
List<Menu> menuList=menuDAOImpl.findByParentId(parentId);
ObjectMapper mapper = new ObjectMapper();
//用jackson将bean转换为map
returnList=mapper.convertValue(menuList,new TypeReference<List<Map<String, String>>>(){});

3.fastjson的序列化与反序列化

JSONObject jsonObj = JSON.parseObject(s); 

String str = JSON.toJSONString(jsonObj);

.net的序列化与反序列化

JSON 字符串 与 java 对象的转换

1.把java 对象列表转换为json对象数组,并转为字符串
   JSONArray array = JSONArray.fromObject(list);
   String jsonstr = array.toString();

2.把java对象转换成json对象,并转化为字符串
  JSONObject object = JSONObject.fromObject(user);
  Log4jInit.ysulogger.debug(object.toString());

3.把JSON字符串转换为JAVA 对象数组
  JSONArray json = JSONArray.fromObject(userStr);//userStr是json字符串
  List<User> users= (List<User>)JSONArray.toCollection(json, User.class);

4.把JSON字符串转换为JAVA 对象
 JSONObject jsonobject = JSONObject.fromObject(jsonStr);
 User user= (User)JSONObject.toBean(object,User.class);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值