JSON的全稱是”JavaScript Object Notation”,意思是JavaScript對象表示法,它是一種基於文本,獨立於語言的輕量級數據交換格式。XML也是一種數據交換格式,為什么沒 有選擇XML呢?因為XML雖然可以作為跨平台的數據交換格式,但是在JS(JavaScript的簡寫)中處理XML非常不方便,同時XML標記比數據 多,增加了交換產生的流量,而JSON沒有附加的任何標記,在JS中可作為對象處理,所以我們更傾向於選擇JSON來交換數據。這篇文章主要從以下幾個方 面來說明JSON。
Json 的兩種結構
JSON有兩種表示結構,對象和數組。
對象結構以”{”大括號開始,以”}”大括號結束。中間部分由0或多個以”,”分隔的”key(關鍵字)/value(值)”對構成,關鍵字和值之間以”:”分隔,語法結構如代碼。
{
key1:value1,
key2:value2,
...
}
其中關鍵字是字符串,而值可以是字符串,數值,true,false,null,對象或數組
數組結構以”[”開始,”]”結束。中間由0或多個以”,”分隔的值列表組成,語法結構如代碼。
{
{
key1:value1,
key2:value2
},
{
key3:value3,
key4:value4
}
}
認識 JSON 字符串
json字符串:指的是符合json格式要求的js字符串。例如:var jsonStr = "{StudentID:'100',Name:'tmac',Hometown:'usa'}";
json對象:指符合json格式要求的js對象。例如:var jsonObj = { StudentID: "100", Name: "tmac", Hometown: "usa" };
操作 JSON 字符串
在操作 json 之前,需要在 Manager NuGet Package 上安裝 Newtonsoft.Json ,在cs 文件中引用
using Newtonsoft.Json;
1. 讀取 json

string folder = @"..\..\JsonFile\resourse.zh-CN.json"; //read the json
var sourceContent = File.ReadAllText(folder); //parse as array
var sourceobjects = JArray.Parse("[" + sourceContent + "]"); JObject source = JObject.Parse(sourceContent);
2. 獲取 json 中的值

public static void tranversJToken(JToken token, string propName, ref Dictionary<string, string> stringsList) { var prop = token as JProperty; if (prop != null) { propName = propName + "_" + prop.Name; } if (prop != null && prop.Value.GetType().Name.ToLower().Equals("jvalue")) { string _propName = propName.Substring(1); string _prop = prop.Value.ToString(); stringsList[_propName] = _prop; return; } foreach (JToken child in token.Children()) { tranversJToken(child, propName, ref stringsList); } }
3.在已存在 json 文件中添加 json 數據 。用 JObject add 方法添加數據,在最后以 string 類型寫入進文件

using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; namespace Json { class AddJson { /// <summary>
/// Read the other json file value add to local json /// </summary>
public static void addJson() { //read the other json
var sourceContent = File.ReadAllText(@"..\..\JsonFile\resourse.json"); var sourceobjects = JArray.Parse("[" + sourceContent + "]"); // parse as array
JObject source = JObject.Parse(sourceContent); //read the local json
string p = @"..\..\JsonFile\resourse.zh-CN.json"; var content = File.ReadAllText(p); var objects = JArray.Parse("[" + content + "]"); // parse as array
JObject o = JObject.Parse(content); //foreach the JObject add the value
foreach (JToken child in source.Children()) { var prop = child as JProperty; string jsonText = prop.Value.ToString(); JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); if (prop.Name == "choosecolor") { //add the json value to local JOnbject
o.Add(prop.Name, new JObject(jo)); } } //found the file exist
if (!File.Exists(p)) { FileStream fs1 = new FileStream(p, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } //write the json to file
File.WriteAllText(p, o.ToString()); } } }
4.創建新的 json 文件

using Newtonsoft.Json.Linq; using System.IO; namespace Json { class CreateNewJson { public static void CreateJson() { JObject source = new JObject(); source.Add("Name", "yanzhiyi"); string p = @"..\..\NewJson\Create.json"; //found the file exist if (!File.Exists(p)) { FileStream fs1 = new FileStream(p, FileMode.Create, FileAccess.ReadWrite); fs1.Close(); } //write the json to file File.WriteAllText(p, source.ToString()); } } }