一、JSON语法
JSON:JavaScript 对象表示法(JavaScript Object Notation),JSON 是存储和交换文本信息的语法。类似 XML,但 比 XML 更小、更快,更易解析。
JSON 数据的书写格式是:名称/值对。
名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:
"firstName" : "John"
JSON 数组在方括号中书写:
数组可包含多个对象:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
二、GSON简介
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。下载jar包:javascript:void(0)。在eclipse中新建一个java project,把gson-2.2.4.jar引入。在工程根目录下创建一个employee.json文件:
{
"department":"design",
"employees": [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName":"Carter" }
]
}
二、读取json
首先创建一个json解析器,然后创建一个JsonObject对象,之后调用get方法访问属性值:
package ucas.json.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class JsonTest {
public static void main(String[] args) {
try {
JsonParser jParser = new JsonParser();
JsonObject jObject = (JsonObject) jParser
.parse(new FileReader("employee.json"));
System.out.println(jObject.get("department").getAsString());
System.out.println(jObject.get("employees"));
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
输出结果:
design
[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]
可以看到”employees”属性为数组,转化为JsonArray然后循环读取:
package ucas.json.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
public class JsonTest {
public static void main(String[] args) {
try {
JsonParser jParser = new JsonParser();
JsonObject jObject = (JsonObject) jParser
.parse(new FileReader("employee.json"));
System.out.println(jObject.get("department").getAsString());
JsonArray jArray=(JsonArray) jObject.get("employees");
for (int i = 0; i < jArray.size(); i++) {
System.out.println("-----------");
JsonObject jObject2=(JsonObject) jArray.get(i);
System.out.println("id:"+jObject2.get("id").getAsString());
System.out.println("firstName:"+jObject2.get("firstName").getAsString());
System.out.println("lastName:"+jObject2.get("lastName").getAsString());
}
} catch (JsonIOException e) {
e.printStackTrace();
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
输出结果:
design
-----------
id:1
firstName:Bill
lastName:Gates
-----------
id:1
firstName:George
lastName:Bush
-----------
id:3
firstName:Thomas
lastName:Carter
三、创建json
创建json对象,添加属性使用addProperty方法,添加对象使用add方法,例子:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public class JsonDemo2 {
public static void main(String[] args) {
// 创建json数据
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("cat", "it");
JsonArray jArray = new JsonArray();
JsonObject lan1 = new JsonObject();
lan1.addProperty("id", 1);
lan1.addProperty("name", "java");
lan1.addProperty("ide", "eclipse");
jArray.add(lan1);
JsonObject lan2 = new JsonObject();
lan2.addProperty("id", 2);
lan2.addProperty("name", "C++");
lan2.addProperty("ide", "VC6.0");
jArray.add(lan2);
JsonObject lan3 = new JsonObject();
lan3.addProperty("id", 3);
lan3.addProperty("name", "C#");
lan3.addProperty("ide", "Visual Studio");
jArray.add(lan3);
JsonObject lan4 = new JsonObject();
lan4.addProperty("id", 4);
lan4.addProperty("name", "Swift");
lan4.addProperty("ide", "Xcode");
jArray.add(lan4);
jsonObject.add("languages", jArray);
jsonObject.addProperty("pop", true);
System.out.println(jsonObject);
File jsonFile = new File("test.json");
try {
jsonFile.createNewFile();
FileOutputStream fos = new FileOutputStream(jsonFile);
OutputStreamWriter osq = new OutputStreamWriter(fos);
BufferedWriter bfw = new BufferedWriter(osq);
bfw.write(jsonObject.toString());
bfw.close();
osq.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这样在工程根目录下会生成一个test.json的文件:
{"cat":"it","languages":[{"id":1,"name":"哈哈","ide":"eclipse\n"},{"id":2,"name":"C++","ide":"VC6.0"},{"id":3,"name":"C#","ide":"Visual Studio"},{"id":4,"name":"Swift","ide":"Xcode"}],"pop":true}