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. hadoop1.x异常

    可能是存在class冲突 http://stackoverflow.com/questions/25130799/hadoop-java-io-ioexception-call-to-localhos ...

  2. PCI配置空间简介

    一.PCI配置空间简介 PCI有三个相互独立的物理地址空间:设备存储器地址空间.I/O地址空间和配置空间.配置空间是PCI所特有的一个物理空间.由于PCI支持设备即插即用,所以PCI设备不占用固定的内 ...

  3. go语言的unsafe包(转)

    The unsafe Package in Golang Golang的unsafe包是一个很特殊的包. 为什么这样说呢? 本文将详细解释. 来自go语言官方文档的警告 unsafe包的文档是这么说的 ...

  4. android学习-异步消息处理机制

    消息处理机制主要对象:Looper,Handler,Message(还有MessageQueue和Runnable) Looper不断从MessageQueue消息队列中取出一个Message,然后传 ...

  5. Hive use mapreduce引擎 bsonFile splits报错处理

    Hive use mapreduce引擎 1.          出现报错.关于bsonFile的.禁止hdfs里面存储的bson文件自动splits. 可以在hive-site.xml文件里面添加如 ...

  6. ruby大神与菜鸟的代码区别

    之前Brand类   has_and_belongs_to_many :categories, index: true 现在在Category类 增加 has_and_belongs_to_many ...

  7. Rails中实现批量删除

    在Rails生成的控制器模版中,包含的destroy只能处理单个对象,而批量删除要求能够同时处理多个对象,这需要自定义一个批量操作action.批量删除的效果图如下:

  8. eclipse + maven 环境配置

    软件152 余建强 第一步:准备以下软件并进行安装 1. jdk1.7或者以上为最佳: 官方下载地址:http://www.oracle.com/technetwork/java/javase/dow ...

  9. session中用户信息改变问题

    问题描述: 在web项目中,我们经常将用户登录信息放在session中用来做后续的权限判断等操作,但最近在项目中发现一个奇怪的现象,session中的用户信息和登录时的信息有了差异. 原因: 在后台代 ...

  10. APICloud APP前端框架——手机APP开发、APP制作、APP定制平台

    概述 APICloud前端框架,包括api.js和api.css.api.css处理不同平台浏览器的默认样式.api.js是一个JavaScript库.是APICloud为混合移动开发定制的轻量Jav ...