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之间的互转的更多相关文章

  1. JSONObject,JSONArray,Map,String之间转换

    http://blog.csdn.net/superit401/article/details/51727739 1.String转JSONObject String jsonMessage = &q ...

  2. java和js中JSONObject,JSONArray,Map,String之间转换

    --------------------------------------------------Java中--------------------------------------------- ...

  3. JSON对象和String之间的互转及处理

    如题,本文列举了一些在web前端开发中常用的转换及处理方式.使用JSON而不是字符串,主要是为了方便处理. JSON:JavaScript 对象表示法(JavaScript Object Notati ...

  4. int 和String之间的互转

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  5. List<Map<String, Object>> 与 json 互转

    近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中.      2.新增或删除指纹时,查询指纹,存到txt缓存文 ...

  6. C++实现json字符串与map的转换

    开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...

  7. fastjson对象,JSON,字符串,map之间的互转

    1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...

  8. 总结的一些json格式和对象/String/Map/List等的互转工具类

    总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...

  9. Json、JavaBean、String等互转

    Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...

随机推荐

  1. EF 一对一、一对多、多对多配置语句小记

    数据库实体间的关系无非有这么几种:一对一.一对多.多对多,这些关系在EF框架中分别有不同的创建方式: 1.在"Database First"模式中,这些关系通过SQL语句的方式建立 ...

  2. Android 开发工具类 20_DOMPersonService

    xml 文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <person i ...

  3. ZigBee 学习资源

    1.雪帕的主页 http://home.cnblogs.com/u/yqh2007/ 2.刘志鹏的主页 http://www.cnblogs.com/hustlzp/archive/2011/02/1 ...

  4. linux安装unzip及使用

    安装完linux ,发现没有UNZIP,没办法,重新安装. 1.获取unzip源码 sudo wget http://downloads.sourceforge.net/infozip/unzip55 ...

  5. AAAI2019 | 基于区域分解集成的目标检测 论文解读

    Object Detection based on Region Decomposition and Assembly AAAI2019 | 基于区域分解集成的目标检测 论文解读 作者 | 文永亮 学 ...

  6. C#的SubString(int start,int end);

    C#的SubString(); 例子: string str = "i am a girl"; string temp = str.Substring(2,2);//从索引2开始, ...

  7. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  8. [笔记] Python 图片转字符画

    一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0  Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...

  9. redis High Availability---Redis Sentinel翻译

    注意,本文档为最新(11月21日),旧版本的可以参考:http://redis.io/topics/sentinel-old 不鼓励使用旧版本的文档. Redis Sentinel是一个用来管理Red ...

  10. vuex中怎么把‘库’中的状态对象赋值给内部对象(三种方法)

    一.通过computed的计算属性直接赋值 import store from '@/store/store' export default{ name: 'count', data(){ retur ...