|
在JS中我們可以直接定義一個對象:
復制代碼 代碼如下:
var obj={name:"藍色",age:10};
上面其實就是所謂的JSON了,我引用下別人的復雜點的例子:
復制代碼 代碼如下:
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@jb51.NET" }
復制代碼 代碼如下:
{ people: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@jb51.NET" },
{ firstName: "Jason", lastName:"Hunter", email: "jb51.NET" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@jb51.NET" }
]}
{ people1: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@jb51.NET" },
{ firstName: "Jason", lastName:"Hunter", email: "jb51.NET" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@jb51.NET" }
],
people2: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@jb51.NET" },
{ firstName: "Jason", lastName:"Hunter", email: "jb51.NET" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@jb51.NET" }
],
people3: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@jb51.NET" },
{ firstName: "Jason", lastName:"Hunter", email: "jb51.NET" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@jb51.NET" }
]
}
2.后臺傳輸進來的滿足JSON條件的文本格式化。
因為JSON最大的用途是AJAX應用,因此關鍵是怎樣把文本轉換成Javascript對象,假設我們請求回的文本為data,那么我們如下解析:
復制代碼 代碼如下:
data=data.replace(//n | /r/g,"");
var obj=eval('('+data+')');
因為我們后臺輸出的時候會把空格和回車符也輸出,因此這里要用正則替換掉,然后用eval轉換成對象就可以調用了。
JavaScript技術:簡明json介紹,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。