Java解析json字符串和json对象

一、引入jar包

json常用的解析jar包有三种:Google、Alibaba、json官方

本篇介绍的是json官方的 org.json下的 json-20200518.jar包

    
    
      org.json
      json
      20200518
    
    
    
      com.google.code.gson
      gson
      2.8.6
    
    
    
      com.alibaba
      fastjson
      1.2.72
    

二、API使用

(1)待解析的json字符串

json在线解析:https://www.sojson.com/

String source = "{\"tSpeakUrl\":\"http://openapi.youdao.com/ttsapi?q=Brofjorden&langType=zh-CHS&sign=BDB192C3B71100805BE44500E1B9C8B1&salt=1594364869753&voice=4&format=mp3&appKey=2fddf5eb3360f6c6\",\"RequestId\":\"d596183f-ceb9-460d-864d-f8c41f7d950e\",\"web\":[{\"value\":[\"布罗夫约尔丹\",\"布罗福德港\",\"布罗弗约登\",\"港口名\"],\"key\":\"Brofjorden\"}],\"query\":\"Brofjorden\",\"translation\":[\"Brofjorden\"],\"errorCode\":\"0\",\"dict\":{\"url\":\"yddict://m.youdao.com/dict?le=eng&q=Brofjorden\"},\"webdict\":{\"url\":\"http://m.youdao.com/dict?le=eng&q=Brofjorden\"},\"l\":\"en2zh-CHS\",\"isWord\":false,\"speakUrl\":\"http://openapi.youdao.com/ttsapi?q=Brofjorden&langType=en&sign=BDB192C3B71100805BE44500E1B9C8B1&salt=1594364869753&voice=4&format=mp3&appKey=2fddf5eb3360f6c6\"}\n";

(2)创建 JSONObject 对象

JSONObject jsonObject = new JSONObject(source);
       

(3)JSONObject 转 Map

 Map jsonMap = jsonObject.toMap();

(4)遍历Map

    public static void foreachJsonMap(Map jsonMap) {
        // 遍历jsonMap
        Set> set = jsonMap.entrySet();
        set.forEach( entry -> {
            String key = entry.getKey();
            Object valueObj = entry.getValue();
            System.out.println(key + "\t\t" + valueObj);
        });
    }

(5)解析json字符串对象

    public static Map jsonObjectToMap(Object object) {
        String source = object.toString().substring(1,object.toString().length()-1).replace("=",":");
        JSONObject jsonObject = new JSONObject(source);
        Map objectMap = jsonObject.toMap();
        objectMap.forEach((key,value) -> System.out.println(key + "\t" + value));
        return objectMap;
    }

 

三、测试代码

import org.json.JSONObject;

import java.util.Map;
import java.util.Set;

public class JsonUtil {

    public static void main(String[] args) {
        String source = "{\"tSpeakUrl\":\"http://openapi.youdao.com/ttsapi?q=Brofjorden&langType=zh-CHS&sign=BDB192C3B71100805BE44500E1B9C8B1&salt=1594364869753&voice=4&format=mp3&appKey=2fddf5eb3360f6c6\",\"RequestId\":\"d596183f-ceb9-460d-864d-f8c41f7d950e\",\"web\":[{\"value\":[\"布罗夫约尔丹\",\"布罗福德港\",\"布罗弗约登\",\"港口名\"],\"key\":\"Brofjorden\"}],\"query\":\"Brofjorden\",\"translation\":[\"Brofjorden\"],\"errorCode\":\"0\",\"dict\":{\"url\":\"yddict://m.youdao.com/dict?le=eng&q=Brofjorden\"},\"webdict\":{\"url\":\"http://m.youdao.com/dict?le=eng&q=Brofjorden\"},\"l\":\"en2zh-CHS\",\"isWord\":false,\"speakUrl\":\"http://openapi.youdao.com/ttsapi?q=Brofjorden&langType=en&sign=BDB192C3B71100805BE44500E1B9C8B1&salt=1594364869753&voice=4&format=mp3&appKey=2fddf5eb3360f6c6\"}\n";
        System.out.println(source);

        // 创建json对象
        JSONObject jsonObject = new JSONObject(source);
        // json对象转map
        Map jsonMap = jsonObject.toMap();

        // 根据key返回value值
        Object web = jsonMap.get("web");
        System.out.println(web);

        // 遍历json
        System.out.println("\n=======================================================================================================================================\n");
        foreachJsonMap(jsonMap);
    }

    public static void foreachJsonMap(Map jsonMap) {
        // 遍历jsonMap
        Set> set = jsonMap.entrySet();
        set.forEach( entry -> {
            String key = entry.getKey();
            Object valueObj = entry.getValue();
            System.out.println(key + "\t\t" + valueObj);
            if(key.equals("web")){
//                jsonObjectToMap(valueObj);
            }
        });
    }

    /**
     * 解析json字符串里的对象,放回 Map
     * @param object
     * @return Map
     */
    public static Map jsonObjectToMap(Object object) {
        String source = object.toString().substring(1,object.toString().length()-1).replace("=",":");
        JSONObject jsonObject = new JSONObject(source);
        Map objectMap = jsonObject.toMap();
        objectMap.forEach((key,value) -> System.out.println(key + "\t" + value));
        return objectMap;
    }

}

四、运行效果

Java解析json字符串和json对象_第1张图片

你可能感兴趣的:(Java,Utils,Java基础)