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 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。可以將一個 JSON 字符串轉成一個 Java 對象,或者反過來。下載jar包:http://download.csdn.net/detail/yang_xing_/8363795。在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解析器,然後創建一個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對象,添加屬性使用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}
總結:
json簡介和語法可以參考json官網http://www.json.org/和W3C教程JSON教程 操作json的庫有很多,可以選擇學習。