基础介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。它基于ECMA262语言规范(1999-12第三版)中JavaScript编程语言的一个子集。 JSON采用与编程语言无关的文本格式,但是也使用了类C语言(包括C, C++, C#, Java, JavaScript, Perl, Python等)的习惯,这些特性使JSON成为理想的数据交换格式。
JSON的结构基于下面两点
1. "名称/值"对的集合 不同语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),键列表(keyed list)等,
1. "名称/值"对的集合 不同语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),键列表(keyed list)等,
2. 值的有序列表 多数语言中被理解为数组(array)。
JSON语法规则
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。与C或者Java的字符串非常相似。
JSON示例
JSONObject对象:
{"user":{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}}
JSONArray对象:
{"users":[{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}]}
{"user":{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}}
JSONArray对象:
{"users":[{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}]}
演示程序
import java.io.IOException;
import java.util.Iterator;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.app.json.domain.User;
/**
* 此工具类需要json.jar 支持
* @author Mahc
*/
public class JsonUtil {
private static String jsonObjectStr = "{\"user\":{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}}";
private static String jsonArrayStr = "{\"users\":[" +
"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}," +
"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}," +
"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}]}";
/**
* 生成jsonObjectStr(JSONObject字符串)
* @throws JSONException
*/
public static void generatesJsonObjectStr() throws JSONException{
JSONObject tagJsonObject = new JSONObject();
JSONObject contentJsonObject = new JSONObject();
contentJsonObject.put("name", "hoking");
contentJsonObject.put("mobileNumber", "137****8888");
contentJsonObject.put("address", "Zhengzhou");
tagJsonObject.put("user", contentJsonObject);
System.out.println(tagJsonObject);
}
/**
* 生成jsonArrayStr(JSONArray字符串)
* @throws JSONException
*/
public static void generatesJsonArrayStr() throws JSONException{
JSONObject tagJsonObject = new JSONObject();
JSONArray contentJsonArray = new JSONArray();
for(int i=0;i<3;i++){
JSONObject contentItemJsonObject = new JSONObject();
contentItemJsonObject.put("name", "hoking");
contentItemJsonObject.put("mobileNumber", "137****8888");
contentItemJsonObject.put("address", "Zhengzhou");
contentJsonArray.put(contentItemJsonObject);
}
tagJsonObject.put("users", contentJsonArray);
System.out.println(tagJsonObject);
}
/**
* 解析jsonArrayStr(JSONArray字符串)
* @throws JSONException
*/
public static void analyzeJsonObjectStr() throws JSONException{
JSONObject jsonObjectString = new JSONObject(jsonObjectStr);
JSONObject contentJsonObject = new JSONObject();
contentJsonObject = jsonObjectString.getJSONObject("user");
Iterator iterator = contentJsonObject.keys();
while (iterator.hasNext()) {
String key = (String)iterator.next();
String param = contentJsonObject.getString(key);
System.out.println(key+":"+param);
}
System.out.println();
}
/**
* 解析jsonArrayStr(JSONArray字符串)
* @throws JSONException
*/
public static void analyzeJsonArrayStr() throws JSONException{
JSONObject jsonObjectString = new JSONObject(jsonArrayStr);
JSONArray contentJsonArray = new JSONArray();
contentJsonArray = jsonObjectString.getJSONArray("users");
for(int i=0;i<contentJsonArray.length();i++){
JSONObject contentItemJsonObject = contentJsonArray.getJSONObject(i);
Iterator iterator = contentItemJsonObject.keys();
while (iterator.hasNext()) {
String key = (String)iterator.next();
String param = contentItemJsonObject.getString(key);
System.out.println(key+":"+param);
}
System.out.println();
}
}
/**
* 将User对象的全部属性与数值以Json字符串格式显示
* 重载此方法为object2JsonObjectString(Object obj) 可以实现对任意Bean对象的解析
* 此方法需要jackson-all-1.9.0.jar 支持
* @throws JsonGenerationException
* @throws JsonMappingException
* @throws IOException
*/
public static void object2JsonObjectString() throws JsonGenerationException, JsonMappingException, IOException{
String jsonString = "";
User user = new User();
user.setName("Hoking");
user.setMobileNumber("137****8888");
user.setAddress("Zhengzhou");
ObjectMapper objectMapper = new ObjectMapper();
jsonString = objectMapper.writeValueAsString(user);
System.out.println(jsonString);
}
}
需要辅助支持的Jar文件
json.jar、jackson-all-1.9.0.jar。点击下载!