.Newtonsoft.Json.dll 使用

前端之家收集整理的这篇文章主要介绍了.Newtonsoft.Json.dll 使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
安装:
1 .解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:

Product product = new Product();
 
product.Name = @H_502_25@"Apple";
product.Expiry = new DateTime(2008,12,28);
product.Price = 3.99M;
product.Sizes = new string[] { @H_502_25@"Small",@H_502_25@"Medium",@H_502_25@"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 = @H_502_25@"['JSON!',1,true,{property:'value'}]";
 
JsonReader reader = new JsonReader(new StringReader(jsonText));
 
Console.WriteLine(@H_502_25@"TokenType\t\tValueType\t\tValue");
 
while (reader.Read())
{
     Console.WriteLine(reader.TokenType + @H_502_25@"\t\t" + WriteValue(reader.ValueType) + @H_502_25@"\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(@H_502_25@"JSON!");
writer.WriteValue(1);
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName(@H_502_25@"property");
writer.WriteValue(@H_502_25@"value");
writer.WriteEndObject();
writer.WriteEndArray();
 
writer.Flush();
 
string jsonText = sw.GetStringBuilder().ToString();
 
Console.WriteLine(jsonText);
// ['JSON!',{property:'value'}]

这里会打印出: ['JSON!',{property:'value'}].
原文链接:https://www.f2er.com/json/290522.html

猜你在找的Json相关文章