json字符串转Map、json数组
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数组的更多相关文章
- json字符串转map、json数组演示
公司项目用的IBM封装的json解析,此处采用阿里的fastjson进行演示,代码如下: package com.alphajuns.test; import com.alibaba.fastjson ...
- json字符串转map
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...
- JSON字符串与Map互转
//一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...
- json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值
一.json相关概念 json,全称为javascript object notation,是一种轻量级的数据交互格式.采用完全独立于语言的文本格式,是一种理想的数据交换格式. 同时,json是jav ...
- VBScript把json字符串解析成json对象的2个方法
这篇文章主要介绍了VBScript把json字符串解析成json对象的2个方法,本文通过MSScriptControl.ScriptControl和jscript实现,需要的朋友可以参考下 asp/v ...
- HttpServletResponse 返回的json数据不是json字符串,而是json对象
今天在改一个bug 情况: 在spring boot中写了一个类Result ,用来统一封装 各个API响应结果 , 其中重写了toString()方法来返回 json字符串 . 在正常情况下,从其它 ...
- json字符串转成 json对象 json对象转换成java对象
import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> ...
- js将json字符串转化成json对象的方法
js将json字符串转化成json对象的方法: JSON.parse(jsonObject)
- nodejs将JSON字符串转化为JSON对象
如何将JSON字符串转化为JSON对象? JSON.parse(str) JSON是javascript的一个内置对象,提供了转换JSON对象与字符串互相转换的方法: 问题来了,道理我都懂 ...
- 特殊字符导致json字符串转换成json对象出错
在对数据库取出来的数据(特别是描述信息)里面含有特殊字符的话,使用JSON.parse将json字符串转换成json对象的时候会出错,主要是双引号,回车换行等影响明显,左尖括号和右尖括号也会导致显示问 ...
随机推荐
- Global.asax.cs中相关方法
protected void Session_Start(object sender, EventArgs e) { #if DEBUG //debug 登陆默认设置 #endif } protect ...
- eclipse创建springBoot项目
创建Spring Boot 工程 先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建1.新建Spring Starter Project 2.Packagin ...
- 增强 用文本增强修改SAP标准屏幕中的字段名称 属于元素的文本增强
如果想要改变标准屏幕中的字段名称,如把物料主数据基本数据元素的名字改为我们想要的名字 . 1.首先,事务MM03进入物料主数据的基本数据2视图中,将鼠标光标放在需要更改的字段“页格式”上,然后按F1键 ...
- 转 Failed to run the WC DB work queue associated with 错误的解决
svn 异常终止导致的缓存工作队列问题 解决方法:清空svn的队列 1.下载sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc.db 3.将sqlite3.exe放到.svn的同级 ...
- HDU3377 Plan
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3377 简单路径要求权值最大,那么为了回避括号序列单独插头的情况特判多,考虑使用最小表示法. #incl ...
- 关于UTC时间和本地时间
收藏了个类Publics 可以实现本地时间和UTC时间的转换 UCT时间=本地时间-8 本地时间比UTC时间快8小时 element-ui的日期选择器上 选择的时间显示的是本地时间 但实 ...
- Ajax同步
转载自:https://blog.csdn.net/xiegongmiao/article/details/78217386 AJAX中根据async的值不同分为同步(async = false)和异 ...
- d3.select(this)不能用箭头函数
d3中典型的数据绑定片段 const items = svg.selectAll('g') .data(gdfs,(d)=> d.name); const enter = items.enter ...
- x1c 2018 体验
总结一下: 2018对比2017优点: 1屏幕完爆:HDR WHD镜面屏完爆 FHD 雾面屏(污+雾,所谓的油腻感),还有色彩!正红色第一次觉得这么好看.别人看得出来看不出来我不知道,至少我能看出来非 ...
- css垂直居中方法
CSS垂直居中的简便方法:{position:absolute;left:0;bottom:0;top:0;right:0;margin:auto;}.