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. 【C#】扩展方法浅谈

    C#3 引入的扩展方法这一个理念. 扩展方法最明显的特征是在方法参数中第一个参数有this声明. 其实C#库中有很多已经是扩展方法了.比如linq中对序列使用的查询语句, where, select等 ...

  2. ElasticSearch——日志工具

    Elasticsearch: 权威指南 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.ht ...

  3. docker 命令随笔

    如果是容器传输文件到本地的话,反过来就好了: docker cp  ID全称:容器文件路径   本地路径 2.进入docker 容器 docker exec -it fw-pay-trade-serv ...

  4. VHDL 类型转换

    STD_LOGIC_1164包集合 函 数 名 功 能 TO_STDLOGICVECTOR(A) 由BIT_VECTOR转换为STD_LOGIC_VECTOR TO_BITVECTOR(A) 由STD ...

  5. python学习 day017打卡 类与类之间的关系

    本节主要的内容: 1.依赖关系 2.关联关系,组合关系,聚合关系 3.继承关系,self到底是什么? 4.类中的特殊成员 一.类与类之间的依赖关系 在面向对象的世界中,类与类中存在以下关系: 1.依赖 ...

  6. Scala语言学习

    Scala的程序被编译成Java字节码(生成class文件),所以可以运行在JVM上,并且可以调用Java的类库,兼容Java程序. Scala 是一门多范式(multi-paradigm)的编程语言 ...

  7. re模块与subprocess模块介绍

    一:re模块       处理正则表达式的模块,正则表达式就是一些带有特殊含义的符号或者符号的组合. 作用:对字符串进行过滤,在一堆字符串中找到你所关心的内容,你就需要告诉计算机你的过滤的 规则是什么 ...

  8. 1.1 vue.js devtools使用教程

    1. vue.js devtools使用教程

  9. sklearn中的train_test_split (随机划分训练集和测试集)

    官方文档:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html ...

  10. ZZNUOJ 2022 摩斯密码

    map打表存一下对应的密码   不会map感觉不好弄这题 #include<stdio.h> #include<string.h> #include<math.h> ...