1 简介
2 Javascript中的简单应用
2.1 Object
2.2 Array
2.3 JSON对象与JSON字符串的转换
2.4 灵活运用JSON对象
3 JAVA服务器端org.json包的应用
3.1 JSONObject
3.2 JSONArray
1简介
JSON(JavaScriptObject Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgramming Language, Standard ECMA-2623rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C,C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
JSON具有以下这些形式:
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true
、false
、 null
、对象(object)或者数组(array)。这些结构可以嵌套。
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。
字符串(string)与C或者Java的字符串非常相似。
数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。
空白可以加入到任何符号之间。
2 Javascript中的简单应用
包括Object和Array,两者可以相互嵌套。
2.1 Object
“名称/值”对的集合。
#声明
空对象:
varempty_object = {};
拥有两个属性的对象:
var stooge = {
"first-name": "Jerome",
"last-name": "Howard"
};
更为复杂的对象:
var flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2004-09-22 14:55",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2004-09-23 10:42",
city: "Los Angeles"
}
};
#读取属性
stooge["first-name"]
flight.departure.IATA
如果没有定义该属性,则返回undefined。
如果设置默认值,可以用 || 。
stooge["first-name"]||""
#编辑属性
stooge["first-name"] = 'Lester';
stooge.nickname = 'Curly';
#删除属性
delete stooge["first-name"];
delete stooge.nickname;
#其他请参照javascript的Object对象相关方法。
2.2 Array
值的有序列表。
#声明
空数组:
var empty =[];
10个值的数组:
var numbers =[
'zero', 'one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine'
];
不同类型元素的数组:
var misc = [
'string', 98.6, true, false, null,undefined,
['nested', 'array'], {object: true}, NaN,
Infinity
];
#其他请参照javascript的Array对象相关方法。
2.3 JSON对象与JSON字符串的转换
因为要与后台交互,所以有必要对JSON对象和JSON字符串进行转换。
#字符串转对象
JSON对象 = eval(JSON字符串);
#对象转字符串
引用1.6版以上的prototype.js
JSON字符串 = Object.toJSON(JSON对象);
2.4 灵活运用JSON对象
比如弹出模式窗口
vReturnValue = window.showModalDialog(sURL[, vArguments] [, sFeatures])
在必要时vReturnValue和vArguments都可以用JSON对象。
比如保存坐标
c = [1,2];或c={x:1,y:2};
3 JAVA服务器端org.json包的应用
包括JSONObject和JSONArray,两者可以相互嵌套。
3.1 JSONObject
#最常用的构造函数
JSONObject() |
JSONObject(java.lang.String source) |
#赋值
put(java.lang.String key, boolean value) | |
put(java.lang.String key, java.util.Collection value) | |
put(java.lang.String key, double value) | |
put(java.lang.String key, int value) | |
put(java.lang.String key, long value) | |
put(java.lang.String key, java.util.Map value) | |
put(java.lang.String key, java.lang.Object value) |
#取值
java.lang.Object | get(java.lang.String key) |
boolean | getBoolean(java.lang.String key) |
double | getDouble(java.lang.String key) |
int | getInt(java.lang.String key) |
getJSONArray(java.lang.String key) | |
getJSONObject(java.lang.String key) | |
long | getLong(java.lang.String key) |
#考虑默认值的取值
java.lang.Object | opt(java.lang.String key) |
boolean | optBoolean(java.lang.String key) |
boolean | optBoolean(java.lang.String key, boolean defaultValue) |
double | optDouble(java.lang.String key) |
double | optDouble(java.lang.String key, double defaultValue) |
int | optInt(java.lang.String key) |
int | optInt(java.lang.String key, int defaultValue) |
optJSONArray(java.lang.String key) | |
optJSONObject(java.lang.String key) | |
long | optLong(java.lang.String key) |
long | optLong(java.lang.String key, long defaultValue) |
java.lang.String | optString(java.lang.String key) |
java.lang.String | optString(java.lang.String key, java.lang.String defaultValue) |
long | getLong(java.lang.String key) |
#删除
java.lang.Object | remove(java.lang.String key) |
#举例
JSONObject jo;
......
jo = new JSONObject();
try {
jo.put("id",item.getId());
jo.put("pkid",item.getPkid());
jo.put("conDate",item.getConDate());
jo.put("topic",item.getTopic());
jo.put("requester",item.getRequester());
jo.put("provider",item.getProvider());
item.setJson(jo.toString());
} catch (JSONException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
......
if (jo.optString("pkid").length() > 0) {
continue;
}
......
conshisBean.setId(jo.optInt("id"));
......
3.2 JSONArray
#最常用的构造函数
JSONArray() |
JSONArray(java.lang.String source) |
#赋值
put(boolean value) | |
put(java.util.Collection value) | |
put(double value) | |
put(int value) | |
put(int index, boolean value) | |
put(int index, java.util.Collection value) | |
put(int index, double value) | |
put(int index, int value) | |
put(int index, long value) | |
put(int index, java.util.Map value) | |
put(int index, java.lang.Object value) | |
put(long value) | |
put(java.util.Map value) | |
put(java.lang.Object value) |
#取值
java.lang.Object | get(int index) |
boolean | getBoolean(int index) |
double | getDouble(int index) |
int | getInt(int index) |
getJSONArray(int index) | |
getJSONObject(int index) | |
long | getLong(int index) |
java.lang.String | getString(int index) |
#考虑默认值的取值
java.lang.Object | opt(int index) |
boolean | optBoolean(int index) |
boolean | optBoolean(int index, boolean defaultValue) |
double | optDouble(int index) |
double | optDouble(int index, double defaultValue) |
int | optInt(int index) |
int | optInt(int index, int defaultValue) |
optJSONArray(int index) | |
optJSONObject(int index) | |
long | optLong(int index) |
long | optLong(int index, long defaultValue) |
java.lang.String | optString(int index) |
java.lang.String | optString(int index, java.lang.String defaultValue) |
#举例
JSONArray ja;
......
ja = new JSONArray ();
try {
ja.put(item.getId());
item.setJson(ja.toString());
} catch (JSONException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
}
......
conshisBean.setId(ja.getInt(1));
......