javascript
json
Newtonsoft.Json.dll/Newtonsoft.Json.Net20.dll
json由N组键值对(名称/值对)组成。一般格式:{key:value,key:value,...}
举个例子:{ 'name': 'simoje', 'sex': '男', 'age': '10', 'is_marry': false, 'childrens': null, 'friends': [ 'tom', 'anly', 'kobe' ], 'friends_age': [ { 'tom': 11 }, { 'anly': 10 }, { 'kobe': '10' } ]}
json支持类型:1、数值2、逻辑值3、数组4、对象5、nullEND
以上为例:var json = { 'name': 'simoje', 'sex': '男', 'age': '10', 'is_marry': false, 'childrens': null, 'friends': [ 'tom', 'anly', 'kobe' ], 'some': [ { 'tom': 11 }, { 'anly': 10 }, { 'kobe': '10' } ]}
获取姓名(name)var name = json['name'];
获取所有的朋友(friends)var friends = json['friends'];var f = '';foreach(var item in firends){ f += item + ',';}f = f.subString(0, f.length - 1);这样就得到了所有的朋友:tom,anly,kobe
获取tom的年龄(friends_age)var tom_age = json['friends_age'][0]['tom'];以上便是json的构造与使用方式了。END
下载Newtonsoft.Json.dll/Newtonsoft.Json.Net20.dll(下方链接),将其添加到项目中,并引用命名空间
声明&赋值JObject json = new JObject( new JProperty('name', 'simoje'), new JProperty('sex', '男'), new JProperty('age', 10), new JProperty('is_marry', false), new JProperty('childrens',null), new JProperty('friends', new JArray( new JValue('tom'), new JValue('anly'), new JValue('kobe'))), new JProperty('friends_age', new JArray( new JProperty('tom', 11), new JProperty('anly', 10), new JProperty('kobe', 10))));
访问&修改var name = json['name'].ToString();
获取姓名(name)JArray friends_ja = JArray.Parse(json['friends'].ToString());var f = string.Empty;foreach (JValue item in friends_ja){ f += item.ToString() + ',';}f = f.Substring(0, f.Length - 1);
获取tom的年龄(friends_age)var friends_age_ja = JArray.Parse(json1['friends_age'].ToString());var tom_age = friends_age_ja[0]['tom'].ToString();
在选择json和xml的过程,实际上是以API的交互方式为准的。