一:在C#中使用json字符串
从这里下载:http://www.newtonsoft.com/products/json/
安装:
1.解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:
Product product = new Product(); product.Name = "Apple";
product.Expiry = new DateTime(, , );
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" }; string output = JavaScriptConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//} Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));
读取JSON
string jsonText = "['JSON!',1,true,{property:'value'}]"; JsonReader reader = new JsonReader(new StringReader(jsonText)); Console.WriteLine("TokenType\t\tValueType\t\tValue"); while (reader.Read())
{
Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))
}
结果显示:
TokenType | ValueType | Value |
---|---|---|
StartArray | null | null |
String | System.String | JSON! |
Integer | System.Int32 | 1 |
Boolean | System.Boolean | True |
StartObject | null | null |
PropertyName | System.String | property |
String | System.String | value |
EndObject | null | null |
EndArray | null | null |
JSON写入
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw); writer.WriteStartArray();
writer.WriteValue("JSON!");
writer.WriteValue();
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName("property");
writer.WriteValue("value");
writer.WriteEndObject();
writer.WriteEndArray(); writer.Flush(); string jsonText = sw.GetStringBuilder().ToString(); Console.WriteLine(jsonText);
// ['JSON!',1,true,{property:'value'}]
这里会打印出: ['JSON!',1,true,{property:'value'}].
完整例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; //需要引用 Newtonsoft.Json.dll
using Newtonsoft.Json; namespace JsonTest
{
class Program
{
/// <summary>
/// 人员类
/// </summary>
public class Person
{
public string name; //姓名
public int age; //年龄
public bool sex_is_male; //性别 public struct Partner //伙伴
{
public string partner_name; //伙伴姓名
public int partner_age; //伙伴年龄
public bool partner_sex_is_male; //伙伴性别
}
public Partner partner; public string[] achievement; //成就
} static void Main(string[] args)
{
//设置一个Person类
Person p = new Person();
p.name = "Tsybius";
p.age = ;
p.sex_is_male = true;
p.partner.partner_name = "Galatea";
p.partner.partner_age = ;
p.partner.partner_sex_is_male = false;
p.achievement = new string[] { "ach1", "ach2", "ach3" }; //直接输出
Console.WriteLine("Formatting.None:");
string json1 = JsonConvert.SerializeObject(p);
Console.WriteLine(json1 + "\n"); //缩进输出
Console.WriteLine("Formatting.Indented:");
string json2 = JsonConvert.SerializeObject(p, Formatting.Indented);
Console.WriteLine(json2 + "\n"); Console.ReadLine();
}
}
}
二:格式
格式:
{"tools": [
{ "name":"css format" , "site":"http://www.cnblogs.com/hongmaju/" },
{ "name":"json format" , "site":"http://www.cnblogs.com/hongmaju/" },
{ "name":"hash MD5" , "site":"http://www.cnblogs.com/hongmaju/" }
]
}
有层次的格式:
{
"": [
{
"Txt": "['收银员']",
"Names": "['bi']",
"data": [
{
"ID": "",
"Name": "收银员"
},
{
"ID": "",
"Name": "张三"
}
]
}
],
"": [
{
"Txt": "['餐台类型']",
"Names": "['ro']",
"data": [
{
"ID": "",
"Name": "A1"
},
{
"ID": "",
"Name": "包桌"
}
]
}
],
"": [
{
"Txt": "['账单类型']",
"Names": "['bi']",
"data": [
{
"ID": "G",
"Name": "挂账账单"
},
{
"ID": "gzhk",
"Name": "挂账回款"
},
{
"ID": "H",
"Name": "会员充值"
},
{
"ID": "l",
"Name": "连锁账单"
},
{
"ID": "M",
"Name": "招待账单"
},
{
"ID": "R",
"Name": "红冲账单"
},
{
"ID": "Z",
"Name": "异地卡充值"
},
{
"ID": "J",
"Name": "预订押金"
},
{
"ID": "Z",
"Name": "已结账单"
}
]
}
],
"": [
{
"Txt": "['营业站点']",
"Names": "['se']",
"data": [
{
"ID": "",
"Name": "重庆万州面馆"
}
]
}
]
}
在线验证json格式的地址:http://www.atool.org/jsonformat.php
三:常用的使用方法
对上面的json的处理:
success: function (result) {
for (var key in result) {
alert(key);//弹出0,1,2,3这几个数字
alert(result[key]);//弹出0对应的json串
alert(getValue(result[key][0].Txt));//得到0对应的json串中的Txt的值
for (var da in result[key]) {
alert(result[key][da].data);//得到0对应的json串中的data的json串
}
}
}