欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSON 客戶端和服務(wù)器端的格式轉(zhuǎn)換

 更新時(shí)間:2009年08月27日 00:28:29   作者:  
JSON是JavaScript Object Notation的縮寫(xiě)。JSON是個(gè)輕量級(jí)的用于服務(wù)器端和客戶端交換數(shù)據(jù)的數(shù)據(jù)格式。它經(jīng)常會(huì)用在ajax應(yīng)用上,是因?yàn)樗腔赼jax對(duì)象的格式定義出來(lái)的。
這里我們會(huì)從javascript的語(yǔ)法引申出json,并在此基礎(chǔ)上引出如何在ajax應(yīng)用程序上使用JSON。
在javascript大家知道有個(gè)Arrays:數(shù)組。它的格式如下:
復(fù)制代碼 代碼如下:

var Beatles = [“Paul”,”John”,”George”,”Ringo”];

上面的實(shí)例一個(gè)Array的形式和下面的等價(jià):
復(fù)制代碼 代碼如下:

var Beatles =new Array [ “Paul”,”John”,”George”,”Ringo”];

再有一個(gè)就是Objects,對(duì)象的創(chuàng)建方式如下:
復(fù)制代碼 代碼如下:

var Beatles = {“Country”:”England”,”YearFormed”:1959,”Style”:”Rock'n'Roll”}

上面這個(gè)創(chuàng)建方式等同于如下的創(chuàng)建方式:
復(fù)制代碼 代碼如下:

var Beatles = new Object();
Beatles.Country = “England”;
Beatles.YearFormad = 1959;
Beatles.Style = “Rock'n'Roll”;

同其它javascript對(duì)象一樣,屬性能夠通過(guò)'.'或'[ ]'來(lái)表示出來(lái)。
Object可以包含Array, 如下:
復(fù)制代碼 代碼如下:

var Beatles = {
“Country” :”England”,
“YeatFormed”: 1959,
“Style” : “Rock'n'Roll”,
“Members”:[ “Paul”,”John”,”George”,”Ringo”]
}

同樣也可以在Array中包含Objects:
復(fù)制代碼 代碼如下:

var Rockbands =[
{
“Name”:”BeatLes”,
“County” : “England”,
“YearFormed” : 1959,
“Style” : “Rock'n'Roll”,
“Members” : [“Paul”,”John”,”George”,”Ringo”]
},
{
“Name” “ “Rolling Stones”,
“Country”:”England”,
“YearFormed”:1962,
“Style” : “Rock'n'Roll”,
“Members” :[“Mick”],”Keith”,”Charlie”,”Bill”]
}
]

在JSON的官網(wǎng)上描述JSON如下:
1. 一個(gè)輕量級(jí)的數(shù)據(jù)轉(zhuǎn)化形式。
2. 容易人們?nèi)プx和寫(xiě)。
3. 容易機(jī)器去解析和生成。
JSON的語(yǔ)法:
JSON對(duì)于一些初級(jí)程序員可能覺(jué)得不好讀和寫(xiě),但是對(duì)于比較有經(jīng)驗(yàn)的人來(lái)說(shuō)還是相當(dāng)不錯(cuò)的(個(gè)人觀點(diǎn))。
雖然JSON和javascript的語(yǔ)法差不多但是它的每個(gè)對(duì)象不能夠賦值給一個(gè)變量。也就是它本身不是哥object而是個(gè)字符串。每次得到JSON時(shí)我們必須通過(guò)轉(zhuǎn)化來(lái)使用它。雖然使用javascript的eval()功能能夠?qū)崿F(xiàn)轉(zhuǎn)換,但是為了安全起見(jiàn)建議大家使用json.js來(lái)轉(zhuǎn)換。可以到上面提供的那個(gè)地址下載。它里面最基本的兩個(gè)方法:
JSON.parse(strJSON)-是用來(lái)轉(zhuǎn)化JSON字符串為JavaScript對(duì)象的、
JSON.stringify(objJSON) –用來(lái)轉(zhuǎn)化一個(gè)JavaScript對(duì)象為一個(gè)JSON對(duì)象的。
這個(gè)是客戶端的數(shù)據(jù)轉(zhuǎn)化,那么服務(wù)器端如何進(jìn)行呢?針對(duì)不同的語(yǔ)言會(huì)有不同的已經(jīng)非常好的轉(zhuǎn)換庫(kù)。因?yàn)楸救耸墙咏?net的所以這里介紹如何使用C#來(lái)進(jìn)行JSON的服務(wù)器端轉(zhuǎn)換。
我看一個(gè)外國(guó)大哥寫(xiě)了一篇 關(guān)于在.net下如何轉(zhuǎn)化JSON的文章相當(dāng)不錯(cuò)。他把JSON.NET和微軟的JavaScriptSerializer集成到一塊,這樣不管你是哪種格式的JSON基本上都能搞定了、
現(xiàn)提供個(gè)簡(jiǎn)單的轉(zhuǎn)化JSON的生成和解析JSON的代碼:
復(fù)制代碼 代碼如下:

public string Serialize(object value)
{
Type type = value.GetType();

Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

json.NullValueHandling = NullValueHandling.Ignore;

json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

if (type == typeof(DataRow))
json.Converters.Add(new DataRowConverter());
else if(type == typeof(DataTable))
json.Converters.Add(new DataTableConverter());
else if (type == typeof(DataSet))
json.Converters.Add(new DataSetConverter());

StringWriter sw = new StringWriter();
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
if (this.FormatJsonOutput)
writer.Formatting = Formatting.Indented;
else
writer.Formatting = Formatting.None;

writer.QuoteChar = '"';
json.Serialize(writer, value);

string output = sw.ToString();
writer.Close();
sw.Close();

return output;
}

public object Deserialize(string jsonText, Type valueType)
{
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

StringReader sr = new StringReader(jsonText);
Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
object result = json.Deserialize(reader, valueType);
reader.Close();

return result;
}

在NET.JSON的基礎(chǔ)上使用JavascriptSeriazible來(lái)轉(zhuǎn)換:
復(fù)制代碼 代碼如下:

internal class WebExtensionsJavaScriptSerializer : JSONSerializerBase, IJSONSerializer
{
public WebExtensionsJavaScriptSerializer(JSONSerializer serializer) : base(serializer)
{}

public string Serialize(object value)
{
JavaScriptSerializer ser = new JavaScriptSerializer();

List<JavaScriptConverter> converters = new List<JavaScriptConverter>();

if (value != null)
{
Type type = value.GetType();
if (type == typeof(DataTable) || type == typeof(DataRow) || type == typeof(DataSet))
{
converters.Add(new WebExtensionsDataRowConverter());
converters.Add(new WebExtensionsDataTableConverter());
converters.Add(new WebExtensionsDataSetConverter());
}

if (converters.Count > 0)
ser.RegisterConverters(converters);
}

return = ser.Serialize(value);
}
public object Deserialize(string jsonText, Type valueType)
{
// *** Have to use Reflection with a 'dynamic' non constant type instance
JavaScriptSerializer ser = new JavaScriptSerializer();


object result = ser.GetType()
.GetMethod("Deserialize")
.MakeGenericMethod(valueType)
.Invoke(ser, new object[1] { jsonText });
return result;
}
}



internal class WebExtensionsDataTableConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] {typeof (DataTable)}; }
}

public override object Deserialize(IDictionary<string, object> dictionary, Type type,
JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}

下篇介紹如何在ASP.NET的環(huán)境下使用JSON和DataTable等的轉(zhuǎn)換,還有介紹使用JQuery的AJAX調(diào)用Web Services的生成JSON、相當(dāng)值得期待。^_^

相關(guān)文章

  • 深入分析JSON編碼格式提交表單數(shù)據(jù)

    深入分析JSON編碼格式提交表單數(shù)據(jù)

    這篇文章主要介紹了深入分析JSON編碼格式提交表單數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • JSON.parse 數(shù)據(jù)不完整的解決方法

    JSON.parse 數(shù)據(jù)不完整的解決方法

    本文主要介紹了JSON.parse 數(shù)據(jù)不完整的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • JSON.stringify 語(yǔ)法實(shí)例講解

    JSON.stringify 語(yǔ)法實(shí)例講解

    可能有些人對(duì)系列化這個(gè)詞過(guò)敏,我的理解很簡(jiǎn)單。就是說(shuō)把原來(lái)是對(duì)象的類型轉(zhuǎn)換成字符串類型(或者更確切的說(shuō)是json類型的)。就這么簡(jiǎn)單。打個(gè)比方說(shuō),你有一個(gè)類,那么你可以通過(guò)這個(gè)方法轉(zhuǎn)換成相應(yīng)的json類型的
    2012-03-03
  • JSON相關(guān)知識(shí)匯總

    JSON相關(guān)知識(shí)匯總

    本文給大家匯總了一下關(guān)于json的相關(guān)的知識(shí)點(diǎn),從基礎(chǔ)到示例,非常全面,有需要的小伙伴可以參考下。
    2015-07-07
  • js+json用表格實(shí)現(xiàn)簡(jiǎn)單網(wǎng)站左側(cè)導(dǎo)航

    js+json用表格實(shí)現(xiàn)簡(jiǎn)單網(wǎng)站左側(cè)導(dǎo)航

    閑暇之余,制作一用表格實(shí)現(xiàn)的簡(jiǎn)單的網(wǎng)站導(dǎo)航條,分享給大家。這里的數(shù)據(jù)基于json格式,學(xué)習(xí)json的朋友可以參考下。
    2010-04-04
  • 解讀JSON的三種格式

    解讀JSON的三種格式

    這篇文章主要介紹了解讀JSON的三種格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • JS解析后臺(tái)返回的JSON格式數(shù)據(jù)實(shí)例

    JS解析后臺(tái)返回的JSON格式數(shù)據(jù)實(shí)例

    今天小編就為大家分享一篇JS解析后臺(tái)返回的JSON格式數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 實(shí)現(xiàn)單層json按照key字母順序排序的示例

    實(shí)現(xiàn)單層json按照key字母順序排序的示例

    下面小編就為大家分享一篇實(shí)現(xiàn)單層json按照key字母順序排序的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 使用JSON作為函數(shù)的參數(shù)的優(yōu)缺點(diǎn)

    使用JSON作為函數(shù)的參數(shù)的優(yōu)缺點(diǎn)

    這篇文章主要介紹了使用JSON作為函數(shù)的參數(shù)的優(yōu)缺點(diǎn),需要的朋友可以參考下
    2016-10-10
  • JSON 入門(mén)教程基礎(chǔ)篇 json入門(mén)學(xué)習(xí)筆記

    JSON 入門(mén)教程基礎(chǔ)篇 json入門(mén)學(xué)習(xí)筆記

    剛開(kāi)始接觸json的時(shí)候感覺(jué)有點(diǎn)奇怪的命名,后來(lái)使用才發(fā)現(xiàn)這么簡(jiǎn)單而且用好用,擴(kuò)展性很強(qiáng),這里就為大家整理一下
    2020-09-09

最新評(píng)論