java Jackson 库操作 json 的基本演示
2015-08-25 14:08
369 查看
核心库下载地址
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
jackson-annotations-2.2.2.jar
jackson-core-2.2.2.jar
jackson-databind-2.2.2.jar
文件类型支持模块
http://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/
jackson-dataformat-xml-2.2.2.jar
导入库
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonParseException;
[java] view
plaincopy
/**
* Map 转换为 json
*/
public static void MyTest01()
{
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "zhang");
hashMap.put("sex", "1");
hashMap.put("login", "Jack");
hashMap.put("password", "123abc");
try
{
ObjectMapper objectMapper = new ObjectMapper();
String userMapJson = objectMapper.writeValueAsString(hashMap);
JsonNode node = objectMapper.readTree(userMapJson);
// 输出结果转意,输出正确的信息
System.out.println(node.get("password").asText());
// 输出不转意,输出结果会包含"",这是不正确的,除非作为json传递,如果是输出结果值,必须如上一行的操作
System.out.println(node.get("name"));
}
catch (IOException e)
{
}
}
[java] view
plaincopy
/**
* 解析 json 格式字符串
*/
public static void MyTest03()
{
try
{
String str = "{\"data\":{\"birth_day\":7,\"birth_month\":6},\"errcode\":0,\"msg\":\"ok\",\"ret\":0}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(str);
JsonNode data = root.path("data");
JsonNode birth_day = data.path("birth_day");
System.out.println(birth_day.asInt());
JsonNode birth_month = data.path("birth_month");
System.out.println(birth_month.asInt());
JsonNode msg = root.path("msg");
System.out.println(msg.textValue());
}
catch (IOException e)
{
}
}
[java] view
plaincopy
/**
* json 直接提取 值
*/
public static void MyTest05()
{
try
{
// 演示字符串
String str = "{\"data\":{\"hasnext\":0,\"info\":[{\"id\":\"288206077664983\",\"timestamp\":1371052476},{\"id\":\"186983078111768\",\"timestamp\":1370944068},{\"id\":\"297031120529307\",\"timestamp\":1370751789},{\"id\":\"273831022294863\",\"timestamp\":1369994812}],\"timestamp\":1374562897,\"totalnum\":422},\"errcode\":0,\"msg\":\"ok\",\"ret\":0,\"seqid\":5903702688915195270}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(str);
// 提取 data
JsonNode data = root.path("data");
// 提取 info
JsonNode info = data.path("info");
System.out.println(info.size());
// 得到 info 的第 0 个
JsonNode item = info.get(0);
System.out.println(item.get("id"));
System.out.println(item.get("timestamp"));
// 得到 info 的第 2 个
item = info.get(2);
System.out.println(item.get("id"));
System.out.println(item.get("timestamp"));
// 遍历 info 内的 array
if (info.isArray())
{
for (JsonNode objNode : info)
{
System.out.println(objNode);
}
}
}
catch (Exception e)
{
}
}
[java] view
plaincopy
/**
* 创建一个 json,并向该 json 添加内容
*/
public static void MyTest07()
{
try
{
ObjectMapper mapper = new ObjectMapper();
ObjectNode root1 = mapper.createObjectNode();
root1.put("nodekey1", 1);
root1.put("nodekey2", 2);
System.out.println(root1.toString());
//Create the root node
ObjectNode root = mapper.createObjectNode ();
//Create a child node
ObjectNode node1 = mapper.createObjectNode ();
node1.put ("nodekey1", 1);
node1.put ("nodekey2", 2);
//Bind the child nodes
root.put ("child", node1);
//Array of nodes
ArrayNode arrayNode = mapper.createArrayNode ();
arrayNode.add (node1);
arrayNode.add (1);
//Bind array node
root.put ("arraynode", arrayNode);
System.out.println (mapper.writeValueAsString (root));
}
catch (Exception e)
{
}
}
json转为map
ObjectMapper mapper = new ObjectMapper();
String json = (String) configMap.get(key);
try {
Map<String,Object> productMap = mapper.readValue(json,Map.class);
//如果是范围的话先不转换
value = (String) productMap.get(value)==null ? value : (String)productMap.get(value);
参考资料
http://wiki.fasterxml.com/JacksonInFiveMinutes
blog.csdn.net/joyous/article/details/9448461
Q群讨论:236201801
http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
jackson-annotations-2.2.2.jar
jackson-core-2.2.2.jar
jackson-databind-2.2.2.jar
文件类型支持模块
http://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/
jackson-dataformat-xml-2.2.2.jar
导入库
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonParseException;
[java] view
plaincopy
/**
* Map 转换为 json
*/
public static void MyTest01()
{
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "zhang");
hashMap.put("sex", "1");
hashMap.put("login", "Jack");
hashMap.put("password", "123abc");
try
{
ObjectMapper objectMapper = new ObjectMapper();
String userMapJson = objectMapper.writeValueAsString(hashMap);
JsonNode node = objectMapper.readTree(userMapJson);
// 输出结果转意,输出正确的信息
System.out.println(node.get("password").asText());
// 输出不转意,输出结果会包含"",这是不正确的,除非作为json传递,如果是输出结果值,必须如上一行的操作
System.out.println(node.get("name"));
}
catch (IOException e)
{
}
}
[java] view
plaincopy
/**
* 解析 json 格式字符串
*/
public static void MyTest03()
{
try
{
String str = "{\"data\":{\"birth_day\":7,\"birth_month\":6},\"errcode\":0,\"msg\":\"ok\",\"ret\":0}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(str);
JsonNode data = root.path("data");
JsonNode birth_day = data.path("birth_day");
System.out.println(birth_day.asInt());
JsonNode birth_month = data.path("birth_month");
System.out.println(birth_month.asInt());
JsonNode msg = root.path("msg");
System.out.println(msg.textValue());
}
catch (IOException e)
{
}
}
[java] view
plaincopy
/**
* json 直接提取 值
*/
public static void MyTest05()
{
try
{
// 演示字符串
String str = "{\"data\":{\"hasnext\":0,\"info\":[{\"id\":\"288206077664983\",\"timestamp\":1371052476},{\"id\":\"186983078111768\",\"timestamp\":1370944068},{\"id\":\"297031120529307\",\"timestamp\":1370751789},{\"id\":\"273831022294863\",\"timestamp\":1369994812}],\"timestamp\":1374562897,\"totalnum\":422},\"errcode\":0,\"msg\":\"ok\",\"ret\":0,\"seqid\":5903702688915195270}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(str);
// 提取 data
JsonNode data = root.path("data");
// 提取 info
JsonNode info = data.path("info");
System.out.println(info.size());
// 得到 info 的第 0 个
JsonNode item = info.get(0);
System.out.println(item.get("id"));
System.out.println(item.get("timestamp"));
// 得到 info 的第 2 个
item = info.get(2);
System.out.println(item.get("id"));
System.out.println(item.get("timestamp"));
// 遍历 info 内的 array
if (info.isArray())
{
for (JsonNode objNode : info)
{
System.out.println(objNode);
}
}
}
catch (Exception e)
{
}
}
[java] view
plaincopy
/**
* 创建一个 json,并向该 json 添加内容
*/
public static void MyTest07()
{
try
{
ObjectMapper mapper = new ObjectMapper();
ObjectNode root1 = mapper.createObjectNode();
root1.put("nodekey1", 1);
root1.put("nodekey2", 2);
System.out.println(root1.toString());
//Create the root node
ObjectNode root = mapper.createObjectNode ();
//Create a child node
ObjectNode node1 = mapper.createObjectNode ();
node1.put ("nodekey1", 1);
node1.put ("nodekey2", 2);
//Bind the child nodes
root.put ("child", node1);
//Array of nodes
ArrayNode arrayNode = mapper.createArrayNode ();
arrayNode.add (node1);
arrayNode.add (1);
//Bind array node
root.put ("arraynode", arrayNode);
System.out.println (mapper.writeValueAsString (root));
}
catch (Exception e)
{
}
}
json转为map
ObjectMapper mapper = new ObjectMapper();
String json = (String) configMap.get(key);
try {
Map<String,Object> productMap = mapper.readValue(json,Map.class);
//如果是范围的话先不转换
value = (String) productMap.get(value)==null ? value : (String)productMap.get(value);
参考资料
http://wiki.fasterxml.com/JacksonInFiveMinutes
blog.csdn.net/joyous/article/details/9448461
Q群讨论:236201801
相关文章推荐
- eclipse连接真机无log,显示log device is empty!的解决办法
- Java开发需要了解
- Spring 3 MVC hello world example
- IDEA Save Actions 类似Eclipse
- git环境搭建(三)——eclipse中的操作
- Java内部类
- Gradle – Spring 4 MVC Hello World Example – Annotation
- eclipse快捷键
- java基于Socket设计一个Mail的收发客户端
- java单例模式 Singleton
- Java反射详解
- 《Java实战开发经典》第五章5.6
- 首个struts2 DEMO制作
- Gradle – Spring 4 MVC Hello World Example
- java 随机数生成工具类
- JDK8引进的JVM参数变化记录
- Hadoop2.7启动报Error: JAVA_HOME is not set and could not be found解决办法
- Java缓冲区之输入输出流
- Zookeeper Api(java)入门与应用
- Java基础笔记-String类