json与list,map,String之间的互转
package tools; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import bean.Student; public class Json {
public static void main(String[] args) {
//json对象转json字符串
jsonObjToJsonStr();
//json字符串转json对象
jsonStrToJsonObj();
//list转json
listToJson();
//json转list
jsonToList();
//json转map
jsonToMap();
//map转json
mapToJson();
} /**
* map转json
*/
private static void mapToJson() {
ObjectMapper mapper = new ObjectMapper();
String json = "";
Map<String, String> map = new HashMap<String,String>();
map.put("name", "JSON");
map.put("age", "23");
map.put("address", "北京市西城区");
try {
json = mapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(json);
} /**
* json转map
*/
private static void jsonToMap() {
String json = "{\"name\":\"JSON\",\"address\":\"北京市西城区\",\"age\":23}";
Map<String, String> map = new HashMap<String, String>();
ObjectMapper mapper = new ObjectMapper();
try{
map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});
System.out.println(map);
}catch(Exception e){
e.printStackTrace();
}
} /**
* json转list
*/
private static void jsonToList() {
//json转成list
String json ="[\"abc\",\"123\"]";
JSONArray jsonArray = JSONArray.fromObject(json);
List<String> list = (List) JSONArray.toCollection(jsonArray);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} /**
* list转json
*/
private static void listToJson() {
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("123");
//list转成json
String json =JSONArray.fromObject(list).toString();
System.out.println(json);
//运行:["abc","123"]
} /**
* json字符串转json对象
*/
private static void jsonStrToJsonObj() {
//json转为json对象,取属性值
String json ="{\"name\":\"JSON\",\"address\":\"北京市西城区\",\"age\":23}";
JSONObject jo = (JSONObject) JSONObject.parse(json);
String add = jo.getString("address");
System.out.println(add);
} /**
* json对象转json字符串
*/
private static void jsonObjToJsonStr() {
Student stu=new Student();
stu.setName("JSON");
stu.setAge(23);
stu.setAddress("北京市西城区");
//对象转换为json字符串
Gson gson = new Gson();
String json = gson.toJson(stu);
System.out.println(json);
}
}
json与list,map,String之间的互转的更多相关文章
- JSONObject,JSONArray,Map,String之间转换
http://blog.csdn.net/superit401/article/details/51727739 1.String转JSONObject String jsonMessage = &q ...
- java和js中JSONObject,JSONArray,Map,String之间转换
--------------------------------------------------Java中--------------------------------------------- ...
- JSON对象和String之间的互转及处理
如题,本文列举了一些在web前端开发中常用的转换及处理方式.使用JSON而不是字符串,主要是为了方便处理. JSON:JavaScript 对象表示法(JavaScript Object Notati ...
- int 和String之间的互转
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- List<Map<String, Object>> 与 json 互转
近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中. 2.新增或删除指纹时,查询指纹,存到txt缓存文 ...
- C++实现json字符串与map的转换
开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...
- fastjson对象,JSON,字符串,map之间的互转
1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...
- 总结的一些json格式和对象/String/Map/List等的互转工具类
总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...
- Json、JavaBean、String等互转
Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...
随机推荐
- vue-devtools必备工具
1.github下载地址:https://github.com/vuejs/vue-devtools 2.下载安成之后打开cmd进入vue-devtools文件夹把依赖装好npm install 之后 ...
- tensorflow基础篇-2
#-*- coding:utf-8 -*- import tensorflow as tf sess=tf.Session() #整流水线单元relu print sess.run(tf.nn.rel ...
- Android 开发工具类 23_getImage
pathText = "http://192.168.1.100:8080/ServerForPicture/wangjialin.jpg" import java.io.Inpu ...
- 梯度下降法的三种形式-BGD、SGD、MBGD
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...
- Go 提高性能的特性
1.值的高效处理和存储,允许创建紧凑的数据结构,避免不必要的填充字节.紧凑的数据结构能更好地利用缓存.更好的缓存利用率可带来更好的性能. 2.函数的调用有开销,减少函数调用开销的解决方案是内联.简单的 ...
- 自动换行的两种代码(C#)
最近有个需求,需要将C# winform中的listBox中的内容自动换行, 其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题, 就是textBox中的文字会 ...
- SpringMVC源码阅读:Json,Xml自动转换
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...
- swiper在vue中的使用 及 神坑
一.实例化和导入 import Swiper from 'swiper'; let viewSwiper; let previewSwiper; 在外面声明全局变量,然后在初始化方法里面实例化swip ...
- 使用gitlab, jenkins搭建CI(持续集成)系统(2) -- 配置webhook触发构建
1. 在gitlab上配置192.168.1.30的ssh秘钥,使jenkins可以操作gitlab上的project 进入gitlab,点击右上角 点击 Settings -> SSH key ...
- lua热更框架之XLua
框架介绍 xLua是当下最流行的unity热更方案之一,作者是腾讯的车雄生前辈,自2016年初推出以来,已经在腾讯的多款游戏项目上应用,目前xLua已经开源到了GitHub.xLua最大的特色是不仅支 ...