→ mepowerlmay:Json.Net 12/29 23:58
※ 引述《supercygnus (......)》之銘言:
: 看了一些市面上的書
: 都沒有找到有關於講json的書
: 想請問各位大大有沒有關於這方面的書
: google了json並沒有書名為json
: 特別上來請益
XML表示法
<school>
<ntust>118</ntust>
<ntu> 114</ntu>
</school>
----------------------------
JavaScript Object Notation (JSON)
Json表示法
{
"school":{
"ntust":"118",
"ntu" :"114"
}
}
---------------------------
JSON 類似map的資料結構 由key跟value組成
key一定是字串 value則可能是字串 或者 集合
在.net原生Json中是以 JsonObject去表現集合
透過繼承了IDictionary, ICollection, IEnumerable
http://msdn.microsoft.com/zh-tw/library/system.json.jsonobject%28v=vs.95%29.aspx
去實作它的資料結構設計
而Json.net中集合則是以JObject來表達
所以上例中的json 使用json.net的json資料結構
add資料方法 可以寫成
JObject json = new JObject();
json.add("school", new JObject());
json["school"].add("ntust", "118");
json["school"].add("ntu", "114");
find方法則為
string key = "ntu";
if(json["school"] != null)
if(json["school"][key] != null)
string num = json["school"][key].toString();
而json又可以在value中同時存在 字串 跟 集合
例如
{
"school":{
"ntust":"118",
"ntu" :"114"
},
"country":"tw"
}
上例的查找方法 可以寫成
if(json["country"] != null)
string country = json["country"].toString();
else
json.add("country", "tw");
並且可以透過try..catch跟parse去檢查是否為一個合法json
bool TryParseJson(String jsonString, out JObject result)
{
JObject json = null;
bool parseResult = false;
try
{
json = JObject.Parse(jsonString);
parseResult = true;
}
catch{}
result = json;
return parseResult;
}
希望能對你有幫助:)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.240.239.100