json tutorials
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?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-fastjson-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>json-fastjson-example</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.1.36</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package org.json.fastjson.example;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
public class FastjsonDecode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String jsonString = "{\"id\":0,\"name\":\"admin\",\"users\":[{\"id\":2,\"name\":\"guest\"},{\"id\":3,\"name\":\"root\"}]}";
|
||||
Group group = JSON.parseObject(jsonString, Group.class);
|
||||
|
||||
System.out.println(group.getId());
|
||||
System.out.println(group.getName());
|
||||
List<User> users = group.getUsers();
|
||||
for (User user : users) {
|
||||
System.out.println(user.getId()+"\t"+user.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package org.json.fastjson.example;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
public class FastjsonEncode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Group group = new Group();
|
||||
group.setId(0L);
|
||||
group.setName("admin");
|
||||
|
||||
User guestUser = new User();
|
||||
guestUser.setId(2L);
|
||||
guestUser.setName("guest");
|
||||
|
||||
User rootUser = new User();
|
||||
rootUser.setId(3L);
|
||||
rootUser.setName("root");
|
||||
|
||||
group.getUsers().add(guestUser);
|
||||
group.getUsers().add(rootUser);
|
||||
|
||||
String jsonString = JSON.toJSONString(group);
|
||||
|
||||
System.out.println(jsonString);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.json.fastjson.example;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Group {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private List<User> users = new ArrayList<User>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.json.fastjson.example;
|
||||
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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-gson-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>json-gson-example</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>1.7.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.json.gson.example;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DataObject implements Serializable{
|
||||
|
||||
private int data1 = 100;
|
||||
private String data2 = "hello";
|
||||
|
||||
private List<String> list = new ArrayList<String>() {
|
||||
{
|
||||
add("String 1");
|
||||
add("String 2");
|
||||
add("String 3");
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
|
||||
+ list + "]";
|
||||
}
|
||||
|
||||
public int getData1() {
|
||||
return data1;
|
||||
}
|
||||
|
||||
public void setData1(int data1) {
|
||||
this.data1 = data1;
|
||||
}
|
||||
|
||||
public String getData2() {
|
||||
return data2;
|
||||
}
|
||||
|
||||
public void setData2(String data2) {
|
||||
this.data2 = data2;
|
||||
}
|
||||
|
||||
public List<String> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<String> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.json.gson.example;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class Gson2Json {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
DataObject obj = new DataObject();
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
// convert java object to JSON format,
|
||||
// and returned as JSON formatted string
|
||||
String json = gson.toJson(obj);
|
||||
|
||||
try {
|
||||
//write converted json data to a file named "file.json"
|
||||
FileWriter writer = new FileWriter("src/main/resources/file.json");
|
||||
writer.write(json);
|
||||
writer.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println(json);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package org.json.gson.example;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class GsonFromJson {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader("src/main/resources/file.json"));
|
||||
// convert the json string back to object
|
||||
DataObject obj = gson.fromJson(br, DataObject.class);
|
||||
System.out.println(obj);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package org.json.gson.example;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
public class GsonStreamReaderExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
JsonReader reader = new JsonReader(new FileReader("src/main/resources/user.json"));
|
||||
|
||||
reader.beginObject();
|
||||
|
||||
while (reader.hasNext()) {
|
||||
|
||||
String name = reader.nextName();
|
||||
|
||||
if (name.equals("name")) {
|
||||
|
||||
System.out.println(reader.nextString());
|
||||
|
||||
} else if (name.equals("age")) {
|
||||
|
||||
System.out.println(reader.nextInt());
|
||||
|
||||
} else if (name.equals("messages")) {
|
||||
|
||||
// read array
|
||||
reader.beginArray();
|
||||
|
||||
while (reader.hasNext()) {
|
||||
System.out.println(reader.nextString());
|
||||
}
|
||||
|
||||
reader.endArray();
|
||||
|
||||
} else {
|
||||
reader.skipValue(); // avoid some unhandle events
|
||||
}
|
||||
}
|
||||
|
||||
reader.endObject();
|
||||
reader.close();
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package org.json.gson.example;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
public class GsonStreamWriterExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
JsonWriter writer;
|
||||
try {
|
||||
writer = new JsonWriter(new FileWriter("src/main/resources/user.json"));
|
||||
|
||||
writer.beginObject(); // {
|
||||
writer.name("name").value("mkyong"); // "name" : "mkyong"
|
||||
writer.name("age").value(29); // "age" : 29
|
||||
|
||||
writer.name("messages"); // "messages" :
|
||||
writer.beginArray(); // [
|
||||
writer.value("msg 1"); // "msg 1"
|
||||
writer.value("msg 2"); // "msg 2"
|
||||
writer.value("msg 3"); // "msg 3"
|
||||
writer.endArray(); // ]
|
||||
|
||||
writer.endObject(); // }
|
||||
|
||||
writer.close();
|
||||
|
||||
System.out.println("Done");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"data1": 100,
|
||||
"data2": "hello",
|
||||
"list": [
|
||||
"String 1",
|
||||
"String 2",
|
||||
"String 3"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"name":"mkyong","age":29,"messages":["msg 1","msg 2","msg 3"]}
|
||||
@@ -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"]}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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-json-simple-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>json-json-simple-example</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package org.json.json.simple.example;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
public class JsonSimpleReadExample {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
JSONParser parser = new JSONParser();
|
||||
|
||||
try {
|
||||
|
||||
Object obj = parser.parse(new FileReader("src/main/resources/test.json"));
|
||||
|
||||
JSONObject jsonObject = (JSONObject) obj;
|
||||
|
||||
String name = (String) jsonObject.get("name");
|
||||
System.out.println(name);
|
||||
|
||||
long age = (Long) jsonObject.get("age");
|
||||
System.out.println(age);
|
||||
|
||||
// loop array
|
||||
JSONArray msg = (JSONArray) jsonObject.get("messages");
|
||||
Iterator<String> iterator = msg.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
System.out.println(iterator.next());
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package org.json.json.simple.example;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class JsonSimpleWriteExample {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("name", "mkyong.com");
|
||||
obj.put("age", new Integer(100));
|
||||
|
||||
JSONArray list = new JSONArray();
|
||||
list.add("msg 1");
|
||||
list.add("msg 2");
|
||||
list.add("msg 3");
|
||||
|
||||
obj.put("messages", list);
|
||||
|
||||
try {
|
||||
|
||||
FileWriter file = new FileWriter("src/main/resources/test.json");
|
||||
file.write(obj.toJSONString());
|
||||
file.flush();
|
||||
file.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.print(obj);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"age":100,"name":"mkyong.com","messages":["msg 1","msg 2","msg 3"]}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>json-tutorials</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>json-jackson-example</module>
|
||||
<module>json-gson-example</module>
|
||||
<module>json-json-simple-example</module>
|
||||
<module>json-fastjson-example</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -76,6 +76,7 @@
|
||||
<module>spring-mongodb-tutorial</module>
|
||||
<module>spring-security-tutorial</module>
|
||||
<module>xml-tutorials</module>
|
||||
<module>json-tutorials</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
+20
-19
@@ -1,21 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xml-tutorials</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>xml-stax-example</module>
|
||||
<module>xml-xom-example</module>
|
||||
<module>xml-xstream-example</module>
|
||||
<module>xml-jaxb-example</module>
|
||||
<module>xml-sax-example</module>
|
||||
<module>xml-xmlpull-example</module>
|
||||
<module>xml-dom-example</module>
|
||||
<module>xml-2-properties</module>
|
||||
</modules>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>maven-framework-project</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xml-tutorials</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>xml-stax-example</module>
|
||||
<module>xml-xom-example</module>
|
||||
<module>xml-xstream-example</module>
|
||||
<module>xml-jaxb-example</module>
|
||||
<module>xml-sax-example</module>
|
||||
<module>xml-xmlpull-example</module>
|
||||
<module>xml-dom-example</module>
|
||||
<module>xml-2-properties</module>
|
||||
</modules>
|
||||
</project>
|
||||
Reference in New Issue
Block a user