json tutorials
This commit is contained in:
@@ -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"]}
|
||||
Reference in New Issue
Block a user