json数组转map

public static void main(String[] args){  

        String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
"{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]";
//第一种方式
List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
System.out.println("利用JSONArray中的parse方法来解析json数组字符串");
for(Map<String,String> mapList : listObjectFir){
for (Map.Entry entry : mapList.entrySet()){
System.out.println( entry.getKey() + " " +entry.getValue());
}
}
//第二种方式
List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
System.out.println("利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串");
for(Map<String,String> mapList : listObjectSec){
for (Map.Entry entry : mapList.entrySet()){
System.out.println( entry.getKey() + " " +entry.getValue());
}
}
//第三种方式
JSONArray listObjectThir = JSONArray.parseArray(strArr);
System.out.println("利用JSONArray中的parseArray方法来解析json数组字符串");
for(Object mapList : listObjectThir){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第四种方式
List listObjectFour = JSONArray.parseArray(strArr,Map.class);
System.out.println("利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串");
for(Object mapList : listObjectFour){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第五种方式
JSONArray listObjectFifth = JSONObject.parseArray(strArr);
System.out.println("利用JSONObject中的parseArray方法来解析json数组字符串");
for(Object mapList : listObjectFifth){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第六种方式
List listObjectSix = JSONObject.parseArray(strArr,Map.class);
System.out.println("利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串");
for(Object mapList : listObjectSix){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第七种方式
JSONArray listObjectSeven = JSON.parseArray(strArr);
System.out.println("利用JSON中的parseArray方法来解析json数组字符串");
for(Object mapList : listObjectSeven){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第八种方式
List listObjectEigh = JSONObject.parseArray(strArr,Map.class);
System.out.println("利用JSON中的parseArray方法并指定返回类型来解析json数组字符串");
for(Object mapList : listObjectEigh){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
}

jsonmap转json字符串

public static void main(String[] args) {
Map map = new HashMap();
map.put("msg", "yes");//map里面装有yes
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println("输出的结果是:" + jsonObject);
//3、将json对象转化为json字符串
String result = jsonObject.toString();
System.out.println(result);
}

(其他集合相同)

java 字符串转成 json 数组并且遍历

String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;  // 一个未转化的字符串
JSONArray json = JSONArray.fromObject(str ); // 首先把字符串转成 JSONArray 对象
if(json.size()>0){
for(int i=0;i<json.size();i++){
JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组,把每一个对象转成 json 对象
System.out.println("name:"+job.get("name")) ; // 得到 每个对象中的属性值
}
}

json字符串转Map、json数组的更多相关文章

  1. json字符串转map、json数组演示

    公司项目用的IBM封装的json解析,此处采用阿里的fastjson进行演示,代码如下: package com.alphajuns.test; import com.alibaba.fastjson ...

  2. json字符串转map

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...

  3. JSON字符串与Map互转

    //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...

  4. json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值

    一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...

  5. VBScript把json字符串解析成json对象的2个方法

    这篇文章主要介绍了VBScript把json字符串解析成json对象的2个方法,本文通过MSScriptControl.ScriptControl和jscript实现,需要的朋友可以参考下 asp/v ...

  6. HttpServletResponse 返回的json数据不是json字符串,而是json对象

    今天在改一个bug 情况: 在spring boot中写了一个类Result ,用来统一封装 各个API响应结果 , 其中重写了toString()方法来返回 json字符串 . 在正常情况下,从其它 ...

  7. json字符串转成 json对象 json对象转换成java对象

    import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> ...

  8. js将json字符串转化成json对象的方法

    js将json字符串转化成json对象的方法: JSON.parse(jsonObject)

  9. nodejs将JSON字符串转化为JSON对象

    如何将JSON字符串转化为JSON对象? JSON.parse(str)       JSON是javascript的一个内置对象,提供了转换JSON对象与字符串互相转换的方法: 问题来了,道理我都懂 ...

  10. 特殊字符导致json字符串转换成json对象出错

    在对数据库取出来的数据(特别是描述信息)里面含有特殊字符的话,使用JSON.parse将json字符串转换成json对象的时候会出错,主要是双引号,回车换行等影响明显,左尖括号和右尖括号也会导致显示问 ...

随机推荐

  1. MySQL like用法

    MySQL LIKE 语法 LIKE运算符用于WHERE表达式中,以搜索匹配字段中的指定内容,语法如下: WHERE column LIKE pattern WHERE column NOT LIKE ...

  2. JMeter中关于动态切换不同CSV文件解决方案

    最近写case,需要当前播放节目的数据作为输入数据,所以每个时刻所用的数据只能是当前时刻附件的数据,尝试用CSV Data Set Config动态加载不同的文件,没有成功,好像CSV Data Se ...

  3. VS 编译后 install报错(error MSB3073)

    vs编译出现如下错误: 错误 1 error MSB3073: 命令"setlocal H:\PCL_BACKUP\PCL\CMake\bin\cmake.exe -DBUILD_TYPE= ...

  4. _itemmod_day_limit

    控制玩家每天获得的物品上限 表说明 `comment` 备注 `entry` 物品 `limitCount`获取上限

  5. java扫描文件夹下面的所有文件(递归与非递归实现)

    java中扫描指定文件夹下面的所有文件扫描一个文件夹下面的所有文件,因为文件夹的层数没有限制可能多达几十层几百层,通常会采用两种方式来遍历指定文件夹下面的所有文件.递归方式非递归方式(采用队列或者栈实 ...

  6. 学习笔记26— roc曲线(python)

    一.概念: 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure 机器学习(ML), 自然语言处理(NLP), 信息检索(IR)等领域, 评估(E ...

  7. Study之6 Neutron(配置使用 Routing)-devstack

    ●Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 firewall 和 floating ip 服务. l3 agent 需要正确 ...

  8. 牛客网NOIP赛前集训营-提高组(第一场)A 中位数

    中位数 思路: 二分答案 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include< ...

  9. 第 8 章 容器网络 - 057 - macvlan 网络隔离和连通

    macvlan 网络隔离和连通 验证 macvlan 之间的连通性. bbox1 能 ping 通 bbox3,bbox2 能 ping 通 bbox4. 即:同一 macvlan 网络能通信. bb ...

  10. Linux chpasswd (批量或单一修改用户密码)和passwd(直接修改用户密码)

    Linux命令:chpasswd 批量或者单一修改用户密码 语法: 1:# echo 用户名:密码 | chpasswd 2:# chpasswd < doiido.txt 实例 1.直接修改d ...