json tutorials
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>json-tutorials</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>json-jackson-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>json-jackson-example</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>codehaus</id>
|
||||
<url>http://repository.codehaus.org/org/codehaus</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jackson</groupId>
|
||||
<artifactId>jackson-mapper-asl</artifactId>
|
||||
<version>1.9.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonEncoding;
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
|
||||
public class JacksonGeneratorExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
|
||||
/*** write to file ***/
|
||||
JsonGenerator jGenerator = jfactory.createJsonGenerator(new File("src/main/resources/user.json"), JsonEncoding.UTF8);
|
||||
jGenerator.writeStartObject(); // {
|
||||
|
||||
jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong"
|
||||
jGenerator.writeNumberField("age", 29); // "age" : 29
|
||||
|
||||
jGenerator.writeFieldName("messages"); // "messages" :
|
||||
jGenerator.writeStartArray(); // [
|
||||
|
||||
jGenerator.writeString("msg 1"); // "msg 1"
|
||||
jGenerator.writeString("msg 2"); // "msg 2"
|
||||
jGenerator.writeString("msg 3"); // "msg 3"
|
||||
|
||||
jGenerator.writeEndArray(); // ]
|
||||
|
||||
jGenerator.writeEndObject(); // }
|
||||
|
||||
jGenerator.close();
|
||||
|
||||
} catch (JsonGenerationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JsonMappingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.JsonToken;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
|
||||
public class JacksonParserExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
JsonFactory jfactory = new JsonFactory();
|
||||
|
||||
/*** read from file ***/
|
||||
JsonParser jParser = jfactory.createJsonParser(new File("src/main/resources/user.json"));
|
||||
|
||||
// loop until token equal to "}"
|
||||
while (jParser.nextToken() != JsonToken.END_OBJECT) {
|
||||
|
||||
String fieldname = jParser.getCurrentName();
|
||||
if ("name".equals(fieldname)) {
|
||||
|
||||
// current token is "name",
|
||||
// move to next, which is "name"'s value
|
||||
jParser.nextToken();
|
||||
System.out.println(jParser.getText()); // display mkyong
|
||||
|
||||
}
|
||||
|
||||
if ("age".equals(fieldname)) {
|
||||
|
||||
// current token is "age",
|
||||
// move to next, which is "name"'s value
|
||||
jParser.nextToken();
|
||||
System.out.println(jParser.getIntValue()); // display 29
|
||||
|
||||
}
|
||||
|
||||
if ("messages".equals(fieldname)) {
|
||||
|
||||
jParser.nextToken(); // current token is "[", move next
|
||||
|
||||
// messages is array, loop until token equal to "]"
|
||||
while (jParser.nextToken() != JsonToken.END_ARRAY) {
|
||||
|
||||
// display msg1, msg2, msg3
|
||||
System.out.println(jParser.getText());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
jParser.close();
|
||||
|
||||
} catch (JsonGenerationException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (JsonMappingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonNode;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.node.ObjectNode;
|
||||
|
||||
public class JacksonTreeNode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader fileReader = new BufferedReader(new FileReader("src/main/resources/user.json"));
|
||||
JsonNode rootNode = mapper.readTree(fileReader);
|
||||
|
||||
/*** read ***/
|
||||
JsonNode nameNode = rootNode.path("name");
|
||||
System.out.println(nameNode.getTextValue());
|
||||
|
||||
JsonNode ageNode = rootNode.path("age");
|
||||
System.out.println(ageNode.getIntValue());
|
||||
|
||||
JsonNode msgNode = rootNode.path("messages");
|
||||
Iterator<JsonNode> ite = msgNode.getElements();
|
||||
|
||||
while (ite.hasNext()) {
|
||||
JsonNode temp = ite.next();
|
||||
System.out.println(temp.getTextValue());
|
||||
}
|
||||
|
||||
/*** update ***/
|
||||
((ObjectNode) rootNode).put("nickname", "new nickname");
|
||||
((ObjectNode) rootNode).put("name", "updated name");
|
||||
((ObjectNode) rootNode).remove("age");
|
||||
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("target/user.json"), rootNode);
|
||||
|
||||
} catch (JsonGenerationException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (JsonMappingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
public class Json2Object {
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
|
||||
// read from file, convert it to user class
|
||||
User user = mapper.readValue(ClassLoader.getSystemResourceAsStream("user.json"), User.class);
|
||||
|
||||
// display to console
|
||||
System.out.println(user);
|
||||
|
||||
} catch (JsonGenerationException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (JsonMappingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
|
||||
public class JsonFile2Map {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// read JSON from a file
|
||||
Map<String, Object> map = mapper.readValue(
|
||||
new File("src/main/resources/user.json"),
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
System.out.println(map.get("name"));
|
||||
System.out.println(map.get("age"));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArrayList<String> list = (ArrayList<String>) map.get("messages");
|
||||
|
||||
for (String msg : list) {
|
||||
System.out.println(msg);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
|
||||
public class JsonString2Map {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String json = "{\"name\":\"mkyong\", \"age\":\"29\"}";
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
|
||||
// convert JSON string to Map
|
||||
map = mapper.readValue(json,new TypeReference<HashMap<String, String>>() {});
|
||||
|
||||
System.out.println(map);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
public class Map2JsonFile {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("name", "mkyong");
|
||||
map.put("age", 29);
|
||||
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
list.add("msg 1");
|
||||
list.add("msg 2");
|
||||
list.add("msg 3");
|
||||
|
||||
map.put("messages", list);
|
||||
|
||||
try {
|
||||
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("src/main/resources/user.json"), map);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
public class Map2JsonString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json = "";
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("name", "mkyong");
|
||||
map.put("age", "29");
|
||||
|
||||
//convert map to JSON string
|
||||
json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
|
||||
|
||||
System.out.println(json);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
public class Object2Json {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
User user = new User();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
|
||||
// convert user object to json string, and save to a file
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("src/main/resources/user.json"), user);
|
||||
|
||||
// display to console
|
||||
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user));
|
||||
|
||||
} catch (JsonGenerationException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (JsonMappingException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.json.jackson.example;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class User implements Serializable{
|
||||
|
||||
private int age = 29;
|
||||
private String name = "mkyong";
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public void setMessages(List<String> messages) {
|
||||
this.messages = messages;
|
||||
}
|
||||
|
||||
private List<String> messages = new ArrayList<String>() {
|
||||
{
|
||||
add("msg 1");
|
||||
add("msg 2");
|
||||
add("msg 3");
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [age=" + age + ", name=" + name + ", " +
|
||||
"messages=" + messages + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"name":"mkyong","age":29,"messages":["msg 1","msg 2","msg 3"]}
|
||||
Reference in New Issue
Block a user