Newtonsoft.Json序列化和反序列

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

Product@H_502_3@ product = new@H_502_3@ Product@H_502_3@();
 
  
product.Name = "Apple"@H_502_3@;
product.Expiry = new@H_502_3@ DateTime@H_502_3@(2008,12,28);
product.Price = 3.99M;
product.Sizes = new@H_502_3@ string@H_502_3@[] { "Small"@H_502_3@,"Medium"@H_502_3@,"Large"@H_502_3@ };
 
  
string@H_502_3@ output = javascriptConvert@H_502_3@.SerializeObject(product);
//{@H_502_3@
// "Name": "Apple",@H_502_3@
// "Expiry": new Date(1230422400000),@H_502_3@
// "Price": 3.99,@H_502_3@
// "Sizes": [@H_502_3@
// "Small",@H_502_3@
// "Medium",@H_502_3@
// "Large"@H_502_3@
// ]@H_502_3@
//}@H_502_3@
 
  
Product@H_502_3@ deserializedProduct = (Product@H_502_3@)javascriptConvert@H_502_3@.DeserializeObject(output,typeof@H_502_3@(Product@H_502_3@));

读取JSON

string@H_502_3@ jsonText = "['JSON!',1,true,{property:'value'}]"@H_502_3@;
 
  
JsonReader@H_502_3@ reader = new@H_502_3@ JsonReader@H_502_3@(new@H_502_3@ StringReader@H_502_3@(jsonText));
 
  
Console@H_502_3@.WriteLine("TokenType\t\tValueType\t\tValue"@H_502_3@);
 
  
while@H_502_3@ (reader.Read())
{
 Console@H_502_3@.WriteLine(reader.TokenType + "\t\t"@H_502_3@ + WriteValue(reader.ValueType) + "\t\t"@H_502_3@ + 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写入@H_301_252@
StringWriter@H_502_3@ sw = new@H_502_3@ StringWriter@H_502_3@();
JsonWriter@H_502_3@ writer = new@H_502_3@ JsonWriter@H_502_3@(sw);
 
  
writer.WriteStartArray();
writer.WriteValue("JSON!"@H_502_3@);
writer.WriteValue(1);
writer.WriteValue(true@H_502_3@);
writer.WriteStartObject();
writer.WritePropertyName("property"@H_502_3@);
writer.WriteValue("value"@H_502_3@);
writer.WriteEndObject();
writer.WriteEndArray();
 
  
writer.Flush();
 
  
string@H_502_3@ jsonText = sw.GetStringBuilder().ToString();
 
  
Console@H_502_3@.WriteLine(jsonText);
// ['JSON!',{property:'value'}]@H_502_3@

这里会打印出: ['JSON!',{property:'value'}].







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;

namespace WebApplication15
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            var jsonBuilder = new System.Text.StringBuilder();
            using (var jsonWriter = new Newtonsoft.Json.JsonTextWriter(new System.IO.StringWriter(jsonBuilder)))
            {
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("status");

                if (false)
                {
                    jsonWriter.WriteValue(false);
                    jsonWriter.WritePropertyName("msg");
                    jsonWriter.WriteValue("未知原因,请重试");
                }
                else
                {
                    jsonWriter.WriteValue(true);
                    jsonWriter.WritePropertyName("yftest");
                    jsonWriter.WriteStartArray();
                    for (int i = 0; i < 10; i++)
                    {
                        jsonWriter.WriteStartObject();
                        jsonWriter.WritePropertyName("id");
                        jsonWriter.WriteValue(i);
                        jsonWriter.WritePropertyName("name");
                        jsonWriter.WriteValue("test");
                        jsonWriter.WriteEndObject();
                    }

                    jsonWriter.WriteEndArray();
                }

                jsonWriter.WriteEndObject();
                jsonWriter.Flush();
            }

            Response.Write(jsonBuilder);

            //获取根status
          //var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString());

          //JToken ageToken = jsonObject["status"];
          //Response.Write(ageToken.ToString());

            //遍历深层参数
            var jsonObject = Newtonsoft.Json.Linq.JObject.Parse(jsonBuilder.ToString());
            var selectToken = jsonObject.SelectTokens("yftest",false);
            //if (selectToken != null && selectToken is Newtonsoft.Json.Linq.JArray)
            //{
                foreach (var item in selectToken.Children())
                {
                 JToken ageToken = item["id"];
                }
            //}
        }
    }
}

猜你在找的Json相关文章