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()); } } }