java获取JSON字符串的key(键)和value(值)

时间:2024-03-04 15:54:04
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapTest {
    public static void main(String[] args) {
        String str="{\'name\':\'小明\',\'sex\':\'男\',\'age\':20}";
        HashMap map = JSON.parseObject(str, HashMap.class);
        for (Object key : map.keySet()) {
            System.out.println(key+" "+map.get(key));
        }
    }
}

  

打印结果: