-
利用阿里巴巴封装的FastJSON来解析json并转换成map
- package com.zkn.newlearn.json;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import java.util.Map;
-
- public class JsonToMapTest01 {
-
- public static void main(String[] args){
-
- String str = "{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}";
-
- Map maps = (Map)JSON.parse(str);
- System.out.println("这个是用JSON类来解析JSON字符串!!!");
- for (Object map : maps.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" " + ((Map.Entry)map).getValue());
- }
-
- Map mapTypes = JSON.parseObject(str);
- System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
- for (Object obj : mapTypes.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapTypes.get(obj));
- }
-
- Map mapType = JSON.parseObject(str,Map.class);
- System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
- for (Object obj : mapType.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapType.get(obj));
- }
-
-
- Map json = (Map) JSONObject.parse(str);
- System.out.println("这个是用JSONObject类的parse方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- JSONObject jsonObject = JSONObject.parseObject(str);
- System.out.println("这个是用JSONObject的parseObject方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- Map mapObj = JSONObject.parseObject(str,Map.class);
- System.out.println("这个是用JSONObject的parseObject方法并执行返回类型来解析JSON字符串!!!");
- for (Object map: json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
- String strArr = "{{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}," +
- "{"00":"zhangsan","11":"lisi","22":"wangwu","33":"maliu"}}";
-
- System.out.println(json);
- }
- }
-
相关阅读:
编程思想之正则表达式
SQL查询顺序
hibernate inverse属性的作用
介绍一下Hibernate的二级缓存
JSON数据
你没玩过的全新版本!Win10这些骚操作你知多少
VSCode 小鸡汤 第01期
Editor REST Client
k8s常用命令
【项目3-2】多肉植物网站
-
原文地址:https://www.cnblogs.com/lsxuejava/p/6826981.html
Copyright © 2020-2023
润新知