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-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"]}
|
||||
Reference in New Issue
Block a user