posts - 0,  comments - 0,  trackbacks - 0
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangable with programming languages also be based on these structures.

In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).



An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

 


A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.



There is an example show how to create a JSON object and operate it.

 

function showJSON() {
    
var student = 
{
        
"username":"zhazha"
,
        
"sex":"male"
,
        
"age":21
,
        
"address":{"city":"guangzhou""postcode":"510006"}
,
        
"selectedCourse"
:[
            
{"cid":"cs001""course":"AI""score":89}
,
            
{"cid":"cs002""course":"Network""score":92}

        ]
    }
;
    
    alert(student.username 
+ "," + student.sex + "," +
 student.age);
    alert(student.address.city 
+ "," +
 student.address.postcode);
    alert(student.selectedCourse[
1].course + "=" + student.selectedCourse[1
].score);
    student.selectedCourse[
1].score = 100
;
    alert(student.selectedCourse[
1].course + "=" + student.selectedCourse[1
].score);
}


To convert a JSON text into an object, use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure.

Your can use eval() function to pares JSON text to JSON Object.

For example:

function TextToJSON() {
    
var studentText = "{'username':'zhazha','sex':'male','age':21," +

        
"'address':{'city':'guangzhou', 'postcode':'510006'}," +
        
"'selectedCourse':[{'cid':'cs001', 'course':'AI', 'score':89}," +
        
"{'cid':'cs002', 'course':'Network', 'score':92}]}";
    
var obj1 = eval('(' + studentText +
 ')');
    alert(obj1.username 
+ "," + obj1.sex + "," +
 obj1.age);
    alert(obj1.address.city 
+ "," +
 obj1.address.postcode);
    alert(obj1.selectedCourse[
1].course + "=" + obj1.selectedCourse[1
].score);
}


A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.

(PS: Please download
json2.js before using the JSON stringifier)

For example:

function JSONToText() {
    
var person = new Person("zhazha""male""21"
);
    alert(JSON.stringify(person));
}


function Person(username, sex, age) {
    
this.username =
 username;
    
this.sex =
 sex;
    
this.age =
 age;
}
posted on 2008-05-02 11:33 邪恶K线图 阅读(206) 评论(0)  编辑  收藏 所属分类: Ajax

只有注册用户登录后才能发表评论。


网站导航: