JSON 数据交换

来源:互联网 发布:qq输入法 linux 编辑:程序博客网 时间:2024/05/18 03:23


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)、truefalsenull、对象(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()
          Construct an empty JSONObject.

JSONObject(java.lang.String source)
          Construct a JSONObject from a source JSON text string.

 

#赋值

 JSONObject

put(java.lang.String key, boolean value)
          Put a key/boolean pair in the JSONObject.

 JSONObject

put(java.lang.String key, java.util.Collection value)
          Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.

 JSONObject

put(java.lang.String key, double value)
          Put a key/double pair in the JSONObject.

 JSONObject

put(java.lang.String key, int value)
          Put a key/int pair in the JSONObject.

 JSONObject

put(java.lang.String key, long value)
          Put a key/long pair in the JSONObject.

 JSONObject

put(java.lang.String key, java.util.Map value)
          Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.

 JSONObject

put(java.lang.String key, java.lang.Object value)
          Put a key/value pair in the JSONObject.

 

#取值

 java.lang.Object

get(java.lang.String key)
          Get the value object associated with a key.

 boolean

getBoolean(java.lang.String key)
          Get the boolean value associated with a key.

 double

getDouble(java.lang.String key)
          Get the double value associated with a key.

 int

getInt(java.lang.String key)
          Get the int value associated with a key.

 JSONArray

getJSONArray(java.lang.String key)
          Get the JSONArray value associated with a key.

 JSONObject

getJSONObject(java.lang.String key)
          Get the JSONObject value associated with a key.

 long

getLong(java.lang.String key)
          Get the long value associated with a key.

 

#考虑默认值的取值

 java.lang.Object

opt(java.lang.String key)
          Get an optional value associated with a key.

 boolean

optBoolean(java.lang.String key)
          Get an optional boolean associated with a key.

 boolean

optBoolean(java.lang.String key, boolean defaultValue)
          Get an optional boolean associated with a key.

 double

optDouble(java.lang.String key)
          Get an optional double associated with a key, or NaN if there is no such key or if its value is not a number.

 double

optDouble(java.lang.String key, double defaultValue)
          Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number.

 int

optInt(java.lang.String key)
          Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.

 int

optInt(java.lang.String key, int defaultValue)
          Get an optional int value associated with a key, or the default if there is no such key or if the value is not a number.

 JSONArray

optJSONArray(java.lang.String key)
          Get an optional JSONArray associated with a key.

 JSONObject

optJSONObject(java.lang.String key)
          Get an optional JSONObject associated with a key.

 long

optLong(java.lang.String key)
          Get an optional long value associated with a key, or zero if there is no such key or if the value is not a number.

 long

optLong(java.lang.String key, long defaultValue)
          Get an optional long value associated with a key, or the default if there is no such key or if the value is not a number.

 java.lang.String

optString(java.lang.String key)
          Get an optional string associated with a key.

 java.lang.String

optString(java.lang.String key, java.lang.String defaultValue)

 long

getLong(java.lang.String key)
          Get the long value associated with a key.

 

#删除

 java.lang.Object

remove(java.lang.String key)
          Remove a name and its value, if present.

 

#举例

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()
          Construct an empty JSONArray.

JSONArray(java.lang.String source)
          Construct a JSONArray from a source JSON text.

 

#赋值

 JSONArray

put(boolean value)
          Append a boolean value.

 JSONArray

put(java.util.Collection value)
          Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

 JSONArray

put(double value)
          Append a double value.

 JSONArray

put(int value)
          Append an int value.

 JSONArray

put(int index, boolean value)
          Put or replace a boolean value in the JSONArray.

 JSONArray

put(int index, java.util.Collection value)
          Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

 JSONArray

put(int index, double value)
          Put or replace a double value.

 JSONArray

put(int index, int value)
          Put or replace an int value.

 JSONArray

put(int index, long value)
          Put or replace a long value.

 JSONArray

put(int index, java.util.Map value)
          Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.

 JSONArray

put(int index, java.lang.Object value)
          Put or replace an object value in the JSONArray.

 JSONArray

put(long value)
          Append an long value.

 JSONArray

put(java.util.Map value)
          Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.

 JSONArray

put(java.lang.Object value)
          Append an object value.

 

#取值

java.lang.Object

get(int index)
          Get the object value associated with an index.

 boolean

getBoolean(int index)
          Get the boolean value associated with an index.

 double

getDouble(int index)
          Get the double value associated with an index.

 int

getInt(int index)
          Get the int value associated with an index.

 JSONArray

getJSONArray(int index)
          Get the JSONArray associated with an index.

 JSONObject

getJSONObject(int index)
          Get the JSONObject associated with an index.

 long

getLong(int index)
          Get the long value associated with an index.

 java.lang.String

getString(int index)
          Get the string associated with an index.

 

#考虑默认值的取值

 java.lang.Object

opt(int index)
          Get the optional object value associated with an index.

 boolean

optBoolean(int index)
          Get the optional boolean value associated with an index.

 boolean

optBoolean(int index, boolean defaultValue)
          Get the optional boolean value associated with an index.

 double

optDouble(int index)
          Get the optional double value associated with an index.

 double

optDouble(int index, double defaultValue)
          Get the optional double value associated with an index.

 int

optInt(int index)
          Get the optional int value associated with an index.

 int

optInt(int index, int defaultValue)
          Get the optional int value associated with an index.

 JSONArray

optJSONArray(int index)
          Get the optional JSONArray associated with an index.

 JSONObject

optJSONObject(int index)
          Get the optional JSONObject associated with an index.

 long

optLong(int index)
          Get the optional long value associated with an index.

 long

optLong(int index, long defaultValue)
          Get the optional long value associated with an index.

 java.lang.String

optString(int index)
          Get the optional string value associated with an index.

 java.lang.String

optString(int index, java.lang.String defaultValue)
          Get the optional string associated with an index.

 

#举例

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));

......

 

原创粉丝点击