zoukankan      html  css  js  c++  java
  • java jackson map2json的使用

    How to convert Java Map to / from JSON (Jackson)

    Published: August 11, 2011 , Updated: August 11, 2011 , Author: mkyong

    In last Jackson example, we show you how to convert user define object to / from JSON data. This time, we are doing the same, but replace user define object with Map.

    Map is more suitable in a simple and short JSON processing, for complex JSON structure, you should always create an user defined object for easy maintainability.

    In this tutorial, we show you how to use Map to construct the JSON data, write it to file. And also how to read JSON from file, and display each value via Map.

    1. Write JSON to file

    Jackson example to use Map to construct the entire JSON data structure, then write it to file named “user.json“.

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.codehaus.jackson.JsonGenerationException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
     
    public class JacksonExample {
         public static void main(String[] args) {
     
    	ObjectMapper mapper = new ObjectMapper();
     
    	Map<String, Object> userInMap = new HashMap<String, Object>();
    	userInMap.put("name", "mkyong");
    	userInMap.put("age", 29);
     
    	List<Object> list = new ArrayList<Object>();
    	list.add("msg 1");
    	list.add("msg 2");
    	list.add("msg 3");
     
    	userInMap.put("messages", list);
     
    	try {
     
    	  // write JSON to a file
    	  mapper.writeValue(new File("c:\\user.json"), userInMap);
     
    	} catch (JsonGenerationException e) {
    	  e.printStackTrace();
    	} catch (JsonMappingException e) {
    	  e.printStackTrace();
    	} catch (IOException e) {
    	  e.printStackTrace();
    	}
         }
    }

    Outputuser.json

    {
    	"age":29,
    	"name":"mkyong",
    	"messages":["msg 1","msg 2","msg 3"]
    }

    2. Read JSON from file

    Read JSON data from file, convert it to Map and display the values.

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Map;
    import org.codehaus.jackson.JsonGenerationException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.type.TypeReference;
     
    public class JacksonExample {
         public static void main(String[] args) {
     
    	ObjectMapper mapper = new ObjectMapper();
     
    	try {
     
    	   // read JSON from a file
    	   Map<String, Object> userInMap = mapper.readValue(
    		new File("c:\\user.json"), 
    		new TypeReference<Map<String, Object>>() {});
     
    	   System.out.println(userInMap.get("name"));
    	   System.out.println(userInMap.get("age"));
     
    	   ArrayList<String> list = 
    		(ArrayList<String>) userInMap.get("messages");
     
    	   for (String msg : list) {
    		System.out.println(msg);
    	   }
     
    	} catch (JsonGenerationException e) {
        	   e.printStackTrace();
    	} catch (JsonMappingException e) {
    	   e.printStackTrace();
    	} catch (IOException e) {
    	   e.printStackTrace();
    	}
         }
    }

    Output

    mkyong
    29
    msg 1
    msg 2
    msg 3

    References

  • 相关阅读:
    [py]str list切片-去除字符串首尾空格-递归思想
    [py]python面向对象的str getattr特殊方法
    [py]python多态-动态语言的鸭子类型
    [py]py2自带Queue模块实现了3类队列
    【Unity技巧】制作一个简单的NPC
    java7 新特性 总结版
    【游戏周边】Unity,UDK,Unreal Engine4或者CryENGINE——我应该选择哪一个游戏引擎
    【Unity Shaders】Transparency —— 使用alpha通道创建透明效果
    记录最近的几个bug
    理解WebKit和Chromium: 调试Android系统上的Chromium
  • 原文地址:https://www.cnblogs.com/lexus/p/2406355.html
Copyright © 2011-2022 走看看