码迷,mamicode.com
首页 > 编程语言 > 详细

Java:JSON和Map相互转换

时间:2019-09-05 18:44:23      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:ann   art   eval   read   reference   name   tail   jar包   print   

Java:JSON与Map相互转换


需要jar包:jackson-core-2.6.0.jar,jackson-databind-2.6.0.jar,jackson-annotations-2.6.0.jar

1. JSON String to Map

package com.inspur.trc.util;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Json2Map {
    public static void main(String[] args) {
        String json = "{\"name\":\"zitong\", \"age\":\"26\"}";
        Map<String, String> map = new HashMap<String, String>();
        ObjectMapper mapper = new ObjectMapper();

        try{
            map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});
            System.out.println(map);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
运行结果:
{age=26, name=zitong}
2. Map to JSON String

package com.inspur.trc.util;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Map2Json {
    public static void main(String[] args){
        try{
            ObjectMapper mapper = new ObjectMapper();
            String json = "";

            Map<String, String> map = new HashMap<String,String>();
            map.put("name", "zitong");
            map.put("age", "26");

            json = mapper.writeValueAsString(map);

            System.out.println(json);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
运行结果:
{"age":"26","name":"zitong"}
3. Map to JSON File

package com.inspur.trc.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Map2JsonFile {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "zitong");
        map.put("age", 29);

        List<Object> list = new ArrayList<Object>();
        list.add("music");
        list.add("football");

        map.put("hobby", list);

        try {
            mapper.writeValue(new File("F:/user.json"), map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
运行结果:F:/user.json
{"age":29,"name":"zitong","hobby":["music","football"]}
4. JSON File To Map

package com.inspur.trc.util;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonFile2Map {
    public static void main(String[] args) {
        try{
            ObjectMapper mapper = new ObjectMapper();

            Map<String, Object> map = mapper.readValue(new File("F:/user.json"),new TypeReference<Map<String, Object>>(){});

            System.out.println("name: " + map.get("name"));
            System.out.println("age: " + map.get("age"));

            @SuppressWarnings("unchecked")
            List<String> list = (List<String>)map.get("hobby");
            System.out.print("hobby: ");
            for(String str : list){
                System.out.print(str+ " ");
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
运行结果:
name: zitong
age: 29
hobby: music football

阅读原文

Java:JSON和Map相互转换

标签:ann   art   eval   read   reference   name   tail   jar包   print   

原文地址:https://www.cnblogs.com/276815076/p/11468722.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!