成人性生交大片免费看视频r_亚洲综合极品香蕉久久网_在线视频免费观看一区_亚洲精品亚洲人成人网在线播放_国产精品毛片av_久久久久国产精品www_亚洲国产一区二区三区在线播_日韩一区二区三区四区区区_亚洲精品国产无套在线观_国产免费www

主頁 > 知識庫 > Json.net 常用使用小結(jié)(推薦)

Json.net 常用使用小結(jié)(推薦)

熱門標簽:臨沂智能電話機器人加盟 西寧呼叫中心外呼系統(tǒng)線路商 蘇州如何辦理400電話 外呼電話機器人成本 網(wǎng)絡(luò)電話外呼系統(tǒng)上海 聯(lián)通官網(wǎng)400電話辦理 400電話辦理怎么樣 地圖標注軟件免費下載 百應(yīng)電話機器人外呼系統(tǒng)

Json.net 常用使用小結(jié)(推薦)

using System;
using System.Linq;
using System.Collections.Generic;

namespace microstore
{
  public interface IPerson
  {
    string FirstName
    {
      get;
      set;
    }
    string LastName
    {
      get;
      set;
    }
    DateTime BirthDate
    {
      get;
      set;
    }
  }
  public class Employee : IPerson
  {
    public string FirstName
    {
      get;
      set;
    }
    public string LastName
    {
      get;
      set;
    }
    public DateTime BirthDate
    {
      get;
      set;
    }

    public string Department
    {
      get;
      set;
    }
    public string JobTitle
    {
      get;
      set;
    }
  }
  public class PersonConverter : Newtonsoft.Json.Converters.CustomCreationConverterIPerson>
  {
    //重寫abstract class CustomCreationConverterT>的Create方法
    public override IPerson Create(Type objectType)
    {
      return new Employee();
    }
  }

  public partial class testjson : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      //if (!IsPostBack)
      //  TestJson();
    }

    #region 序列化
    public string TestJsonSerialize()
    {
      Product product = new Product();
      product.Name = "Apple";
      product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
      product.Price = 3.99M;
      //product.Sizes = new string[] { "Small", "Medium", "Large" };

      //string json = Newtonsoft.Json.JsonConvert.SerializeObject(product); //沒有縮進輸出
      string json = Newtonsoft.Json.JsonConvert.SerializeObject(product, Newtonsoft.Json.Formatting.Indented);
      //string json = Newtonsoft.Json.JsonConvert.SerializeObject(
      //  product, 
      //  Newtonsoft.Json.Formatting.Indented,
      //  new Newtonsoft.Json.JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }
      //);
      return string.Format("p>{0}/p>", json);
    }
    public string TestListJsonSerialize()
    {
      Product product = new Product();
      product.Name = "Apple";
      product.Expiry = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd hh:mm:ss");
      product.Price = 3.99M;
      product.Sizes = new string[] { "Small", "Medium", "Large" };

      ListProduct> plist = new ListProduct>();
      plist.Add(product);
      plist.Add(product);
      string json = Newtonsoft.Json.JsonConvert.SerializeObject(plist, Newtonsoft.Json.Formatting.Indented);
      return string.Format("p>{0}/p>", json);
    }
    #endregion

    #region 反序列化
    public string TestJsonDeserialize()
    {
      string strjson = "{\"Name\":\"Apple\",\"Expiry\":\"2014-05-03 10:20:59\",\"Price\":3.99,\"Sizes\":[\"Small\",\"Medium\",\"Large\"]}";
      Product p = Newtonsoft.Json.JsonConvert.DeserializeObjectProduct>(strjson);

      string template = @"p>ul>
                  li>{0}/li>
                  li>{1}/li>
                  li>{2}/li>
                  li>{3}/li>
                /ul>/p>";

      return string.Format(template, p.Name, p.Expiry, p.Price.ToString(), string.Join(",", p.Sizes));
    }
    public string TestListJsonDeserialize()
    {
      string strjson = "{\"Name\":\"Apple\",\"Expiry\":\"2014-05-03 10:20:59\",\"Price\":3.99,\"Sizes\":[\"Small\",\"Medium\",\"Large\"]}";
      ListProduct> plist = Newtonsoft.Json.JsonConvert.DeserializeObjectListProduct>>(string.Format("[{0},{1}]", strjson, strjson));

      string template = @"p>ul>
                  li>{0}/li>
                  li>{1}/li>
                  li>{2}/li>
                  li>{3}/li>
                /ul>/p>";

      System.Text.StringBuilder strb = new System.Text.StringBuilder();
      plist.ForEach(x =>
        strb.AppendLine(
          string.Format(template, x.Name, x.Expiry, x.Price.ToString(), string.Join(",", x.Sizes))
        )
      );
      return strb.ToString();
    }
    #endregion

    #region 自定義反序列化
    public string TestListCustomDeserialize()
    {
      string strJson = "[ { \"FirstName\": \"Maurice\", \"LastName\": \"Moss\", \"BirthDate\": \"1981-03-08T00:00Z\", \"Department\": \"IT\", \"JobTitle\": \"Support\" }, { \"FirstName\": \"Jen\", \"LastName\": \"Barber\", \"BirthDate\": \"1985-12-10T00:00Z\", \"Department\": \"IT\", \"JobTitle\": \"Manager\" } ] ";
      ListIPerson> people = Newtonsoft.Json.JsonConvert.DeserializeObjectListIPerson>>(strJson, new PersonConverter());
      IPerson person = people[0];

      string template = @"p>ul>
                  li>當前ListIPerson>[x]對象類型:{0}/li>
                  li>FirstName:{1}/li>
                  li>LastName:{2}/li>
                  li>BirthDate:{3}/li>
                  li>Department:{4}/li>
                  li>JobTitle:{5}/li>
                /ul>/p>";

      System.Text.StringBuilder strb = new System.Text.StringBuilder();
      people.ForEach(x =>
        strb.AppendLine(
          string.Format(
            template,
            person.GetType().ToString(),
            x.FirstName,
            x.LastName,
            x.BirthDate.ToString(),
            ((Employee)x).Department,
            ((Employee)x).JobTitle
          )
        )
      );
      return strb.ToString();
    }
    #endregion

    #region 反序列化成Dictionary

    public string TestDeserialize2Dic()
    {
      //string json = @"{""key1"":""zhangsan"",""key2"":""lisi""}";
      //string json = "{\"key1\":\"zhangsan\",\"key2\":\"lisi\"}";
      string json = "{key1:\"zhangsan\",key2:\"lisi\"}";
      Dictionarystring, string> dic = Newtonsoft.Json.JsonConvert.DeserializeObjectDictionarystring, string>>(json);

      string template = @"li>key:{0},value:{1}/li>";
      System.Text.StringBuilder strb = new System.Text.StringBuilder();
      strb.Append("Dictionarystring, string>長度" + dic.Count.ToString() + "ul>");
      dic.AsQueryable().ToList().ForEach(x =>
      {
        strb.AppendLine(string.Format(template, x.Key, x.Value));
      });
      strb.Append("/ul>");
      return strb.ToString();
    }

    #endregion

    #region NullValueHandling特性
    public class Movie
    {
      public string Name { get; set; }
      public string Description { get; set; }
      public string Classification { get; set; }
      public string Studio { get; set; }
      public DateTime? ReleaseDate { get; set; }
      public Liststring> ReleaseCountries { get; set; }
    }
    /// summary>
    /// 完整序列化輸出
    /// /summary>
    public string CommonSerialize()
    {
      Movie movie = new Movie();
      movie.Name = "Bad Boys III";
      movie.Description = "It's no Bad Boys";

      string included = Newtonsoft.Json.JsonConvert.SerializeObject(
        movie,
        Newtonsoft.Json.Formatting.Indented, //縮進
        new Newtonsoft.Json.JsonSerializerSettings { }
      );

      return included;
    }
    /// summary>
    /// 忽略空(Null)對象輸出
    /// /summary>
    /// returns>/returns>
    public string IgnoredSerialize()
    {
      Movie movie = new Movie();
      movie.Name = "Bad Boys III";
      movie.Description = "It's no Bad Boys";

      string included = Newtonsoft.Json.JsonConvert.SerializeObject(
        movie,
        Newtonsoft.Json.Formatting.Indented, //縮進
        new Newtonsoft.Json.JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }
      );

      return included;
    }
    #endregion

    public class Product
    {
      public string Name { get; set; }
      public string Expiry { get; set; }
      public Decimal Price { get; set; }
      public string[] Sizes { get; set; }
    }

    #region DefaultValueHandling默認值
    public class Invoice
    {
      public string Company { get; set; }
      public decimal Amount { get; set; }

      // false is default value of bool
      public bool Paid { get; set; }
      // null is default value of nullable
      public DateTime? PaidDate { get; set; }

      // customize default values
      [System.ComponentModel.DefaultValue(30)]
      public int FollowUpDays { get; set; }

      [System.ComponentModel.DefaultValue("")]
      public string FollowUpEmailAddress { get; set; }
    }
    public void GG()
    {
      Invoice invoice = new Invoice
      {
        Company = "Acme Ltd.",
        Amount = 50.0m,
        Paid = false,
        FollowUpDays = 30,
        FollowUpEmailAddress = string.Empty,
        PaidDate = null
      };

      string included = Newtonsoft.Json.JsonConvert.SerializeObject(
        invoice,
        Newtonsoft.Json.Formatting.Indented,
        new Newtonsoft.Json.JsonSerializerSettings { }
      );
      // {
      //  "Company": "Acme Ltd.",
      //  "Amount": 50.0,
      //  "Paid": false,
      //  "PaidDate": null,
      //  "FollowUpDays": 30,
      //  "FollowUpEmailAddress": ""
      // }

      string ignored = Newtonsoft.Json.JsonConvert.SerializeObject(
        invoice,
        Newtonsoft.Json.Formatting.Indented,
        new Newtonsoft.Json.JsonSerializerSettings { DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore }
      );
      // {
      //  "Company": "Acme Ltd.",
      //  "Amount": 50.0
      // }
    }
    #endregion

    #region JsonIgnoreAttribute and DataMemberAttribute 特性

    public string OutIncluded()
    {
      Car car = new Car
      {
        Model = "zhangsan",
        Year = DateTime.Now,
        Features = new Liststring> { "aaaa", "bbbb", "cccc" },
        LastModified = DateTime.Now.AddDays(5)
      };
      return Newtonsoft.Json.JsonConvert.SerializeObject(car, Newtonsoft.Json.Formatting.Indented);
    }
    public string OutIncluded2()
    {
      Computer com = new Computer
      {
        Name = "zhangsan",
        SalePrice = 3999m,
        Manufacture = "red",
        StockCount = 5,
        WholeSalePrice = 34m,
        NextShipmentDate = DateTime.Now.AddDays(5)
      };
      return Newtonsoft.Json.JsonConvert.SerializeObject(com, Newtonsoft.Json.Formatting.Indented);
    }

    public class Car
    {
      // included in JSON
      public string Model { get; set; }
      public DateTime Year { get; set; }
      public Liststring> Features { get; set; }

      // ignored
      [Newtonsoft.Json.JsonIgnore]
      public DateTime LastModified { get; set; }
    }

    //在nt3.5中需要添加System.Runtime.Serialization.dll引用
    [System.Runtime.Serialization.DataContract]
    public class Computer
    {
      // included in JSON
      [System.Runtime.Serialization.DataMember]
      public string Name { get; set; }
      [System.Runtime.Serialization.DataMember]
      public decimal SalePrice { get; set; }

      // ignored
      public string Manufacture { get; set; }
      public int StockCount { get; set; }
      public decimal WholeSalePrice { get; set; }
      public DateTime NextShipmentDate { get; set; }
    }

    #endregion

    #region IContractResolver特性
    public class Book
    {
      public string BookName { get; set; }
      public decimal BookPrice { get; set; }
      public string AuthorName { get; set; }
      public int AuthorAge { get; set; }
      public string AuthorCountry { get; set; }
    }
    public void KK()
    {
      Book book = new Book
      {
        BookName = "The Gathering Storm",
        BookPrice = 16.19m,
        AuthorName = "Brandon Sanderson",
        AuthorAge = 34,
        AuthorCountry = "United States of America"
      };
      string startingWithA = Newtonsoft.Json.JsonConvert.SerializeObject(
        book, Newtonsoft.Json.Formatting.Indented,
        new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new DynamicContractResolver('A') }
      );
      // {
      //  "AuthorName": "Brandon Sanderson",
      //  "AuthorAge": 34,
      //  "AuthorCountry": "United States of America"
      // }

      string startingWithB = Newtonsoft.Json.JsonConvert.SerializeObject(
        book,
        Newtonsoft.Json.Formatting.Indented,
        new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new DynamicContractResolver('B') }
      );
      // {
      //  "BookName": "The Gathering Storm",
      //  "BookPrice": 16.19
      // }
    }
    public class DynamicContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
    {
      private readonly char _startingWithChar;

      public DynamicContractResolver(char startingWithChar)
      {
        _startingWithChar = startingWithChar;
      }

      protected override IListNewtonsoft.Json.Serialization.JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
      {
        IListNewtonsoft.Json.Serialization.JsonProperty> properties = base.CreateProperties(type, memberSerialization);

        // only serializer properties that start with the specified character
        properties =
          properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();

        return properties;
      }
    }

    #endregion

    //...
  }
}

    #region Serializing Partial JSON Fragment Example
    public class SearchResult
    {
      public string Title { get; set; }
      public string Content { get; set; }
      public string Url { get; set; }
    }

    public string SerializingJsonFragment()
    {
      #region
      string googleSearchText = @"{
        'responseData': {
          'results': [{
            'GsearchResultClass': 'GwebSearch',
            'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton',
            'url': 'http://en.wikipedia.org/wiki/Paris_Hilton',
            'visibleUrl': 'en.wikipedia.org',
            'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org',
            'title': 'b>Paris Hilton/b> - Wikipedia, the free encyclopedia',
            'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia',
            'content': '[1] In 2006, she released her debut album...'
          },
          {
            'GsearchResultClass': 'GwebSearch',
            'unescapedUrl': 'http://www.imdb.com/name/nm0385296/',
            'url': 'http://www.imdb.com/name/nm0385296/',
            'visibleUrl': 'www.imdb.com',
            'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com',
            'title': 'b>Paris Hilton/b>',
            'titleNoFormatting': 'Paris Hilton',
            'content': 'Self: Zoolander. Socialite b>Paris Hilton/b>...'
          }],
          'cursor': {
            'pages': [{
              'start': '0',
              'label': 1
            },
            {
              'start': '4',
              'label': 2
            },
            {
              'start': '8',
              'label': 3
            },
            {
              'start': '12',
              'label': 4
            }],
            'estimatedResultCount': '59600000',
            'currentPageIndex': 0,
            'moreResultsUrl': 'http://www.google.com/search?oe=utf8ie=utf8...'
          }
        },
        'responseDetails': null,
        'responseStatus': 200
      }";
      #endregion

      Newtonsoft.Json.Linq.JObject googleSearch = Newtonsoft.Json.Linq.JObject.Parse(googleSearchText);
      // get JSON result objects into a list
      ListNewtonsoft.Json.Linq.JToken> listJToken = googleSearch["responseData"]["results"].Children().ToList();
      System.Text.StringBuilder strb = new System.Text.StringBuilder();
      string template = @"ul>
                  li>Title:{0}/li>
                  li>Content: {1}/li>
                  li>Url:{2}/li>
                /ul>";
      listJToken.ForEach(x =>
      {
        // serialize JSON results into .NET objects
        SearchResult searchResult = Newtonsoft.Json.JsonConvert.DeserializeObjectSearchResult>(x.ToString());
        strb.AppendLine(string.Format(template, searchResult.Title, searchResult.Content, searchResult.Url));
      });
      return strb.ToString();
    }

    #endregion

    #region ShouldSerialize
    public class CC
    {
      public string Name { get; set; }
      public CC Manager { get; set; }

      //http://msdn.microsoft.com/en-us/library/53b8022e.aspx
      public bool ShouldSerializeManager()
      {
        // don't serialize the Manager property if an employee is their own manager
        return (Manager != this);
      }
    }
    public string ShouldSerializeTest()
    {
      //create Employee mike
      CC mike = new CC();
      mike.Name = "Mike Manager";

      //create Employee joe
      CC joe = new CC();
      joe.Name = "Joe Employee";
      joe.Manager = mike; //set joe'Manager = mike

      // mike is his own manager
      // ShouldSerialize will skip this property
      mike.Manager = mike;
      return Newtonsoft.Json.JsonConvert.SerializeObject(new[] { joe, mike }, Newtonsoft.Json.Formatting.Indented);
    }
    #endregion

    //駝峰結(jié)構(gòu)輸出(小寫打頭,后面單詞大寫)
    public string JJJ()
    {
      Product product = new Product
      {
        Name = "Widget",
        Expiry = DateTime.Now.ToString(),
        Price = 9.99m,
        Sizes = new[] { "Small", "Medium", "Large" }
      };

      string json = Newtonsoft.Json.JsonConvert.SerializeObject(
        product,
        Newtonsoft.Json.Formatting.Indented,
        new Newtonsoft.Json.JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }
      );
      return json;

      //{
      // "name": "Widget",
      // "expiryDate": "2010-12-20T18:01Z",
      // "price": 9.99,
      // "sizes": [
      //  "Small",
      //  "Medium",
      //  "Large"
      // ]
      //}
    }

    #region ITraceWriter
    public class Staff
    {
      public string Name { get; set; }
      public Liststring> Roles { get; set; }
      public DateTime StartDate { get; set; }
    }
    public void KKKK()
    {
      Staff staff = new Staff();
      staff.Name = "Arnie Admin";
      staff.Roles = new Liststring> { "Administrator" };
      staff.StartDate = new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc);

      Newtonsoft.Json.Serialization.ITraceWriter traceWriter = new Newtonsoft.Json.Serialization.MemoryTraceWriter();
      Newtonsoft.Json.JsonConvert.SerializeObject(
        staff,
        new Newtonsoft.Json.JsonSerializerSettings
        {
          TraceWriter = traceWriter,
          Converters = { new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter() }
        }
      );

      Console.WriteLine(traceWriter);
      // 2012-11-11T12:08:42.761 Info Started serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
      // 2012-11-11T12:08:42.785 Info Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
      // 2012-11-11T12:08:42.791 Info Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path 'StartDate'.
      // 2012-11-11T12:08:42.797 Info Started serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
      // 2012-11-11T12:08:42.798 Info Finished serializing System.Collections.Generic.List`1[System.String]. Path 'Roles'.
      // 2012-11-11T12:08:42.799 Info Finished serializing Newtonsoft.Json.Tests.Serialization.Staff. Path ''.
      // 2013-05-18T21:38:11.255 Verbose Serialized JSON: 
      // {
      //  "Name": "Arnie Admin",
      //  "StartDate": new Date(
      //   976623132000
      //  ),
      //  "Roles": [
      //   "Administrator"
      //  ]
      // }
    }
    #endregion

    public string TestReadJsonFromFile()
    {
      Linq2Json l2j = new Linq2Json();
      Newtonsoft.Json.Linq.JObject jarray = l2j.GetJObject4();
      return jarray.ToString();
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace microstore
{
  public class Linq2Json
  {
    #region GetJObject

    //Parsing a JSON Object from text 
    public Newtonsoft.Json.Linq.JObject GetJObject()
    {
      string json = @"{
               CPU: 'Intel',
               Drives: [
                'DVD read/writer',
                '500 gigabyte hard drive'
               ]
              }";
      Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(json);
      return jobject;
    }

    /* 
     * //example:=>
     * 
      Linq2Json l2j = new Linq2Json();
      Newtonsoft.Json.Linq.JObject jobject = l2j.GetJObject2(Server.MapPath("json/Person.json"));
      //return Newtonsoft.Json.JsonConvert.SerializeObject(jobject, Newtonsoft.Json.Formatting.Indented);
      return jobject.ToString();
     */
    //Loading JSON from a file
    public Newtonsoft.Json.Linq.JObject GetJObject2(string jsonPath)
    {
      using (System.IO.StreamReader reader = System.IO.File.OpenText(jsonPath))
      {
        Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.ReadFrom(new Newtonsoft.Json.JsonTextReader(reader));
        return jobject;
      }
    }

    //Creating JObject
    public Newtonsoft.Json.Linq.JObject GetJObject3()
    {
      ListPost> posts = GetPosts();
      Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.FromObject(new
      {
        channel = new
        {
          title = "James Newton-King",
          link = "http://james.newtonking.com",
          description = "James Newton-King's blog.",
          item =
            from p in posts
            orderby p.Title
            select new
            {
              title = p.Title,
              description = p.Description,
              link = p.Link,
              category = p.Category
            }
        }
      });

      return jobject;
    }
    /*
      {
        "channel": {
          "title": "James Newton-King",
          "link": "http://james.newtonking.com",
          "description": "James Newton-King's blog.",
          "item": [{
            "title": "jewron",
            "description": "4546fds",
            "link": "http://www.baidu.com",
            "category": "jhgj"
          },
          {
            "title": "jofdsn",
            "description": "mdsfan",
            "link": "http://www.baidu.com",
            "category": "6546"
          },
          {
            "title": "jokjn",
            "description": "m3214an",
            "link": "http://www.baidu.com",
            "category": "hg425"
          },
          {
            "title": "jon",
            "description": "man",
            "link": "http://www.baidu.com",
            "category": "goodman"
          }]
        }
      }
     */
    //Creating JObject
    public Newtonsoft.Json.Linq.JObject GetJObject4()
    {
      ListPost> posts = GetPosts();
      Newtonsoft.Json.Linq.JObject rss = new Newtonsoft.Json.Linq.JObject(
          new Newtonsoft.Json.Linq.JProperty("channel",
            new Newtonsoft.Json.Linq.JObject(
              new Newtonsoft.Json.Linq.JProperty("title", "James Newton-King"),
              new Newtonsoft.Json.Linq.JProperty("link", "http://james.newtonking.com"),
              new Newtonsoft.Json.Linq.JProperty("description", "James Newton-King's blog."),
              new Newtonsoft.Json.Linq.JProperty("item",
                new Newtonsoft.Json.Linq.JArray(
                  from p in posts
                  orderby p.Title
                  select new Newtonsoft.Json.Linq.JObject(
                    new Newtonsoft.Json.Linq.JProperty("title", p.Title),
                    new Newtonsoft.Json.Linq.JProperty("description", p.Description),
                    new Newtonsoft.Json.Linq.JProperty("link", p.Link),
                    new Newtonsoft.Json.Linq.JProperty("category",
                      new Newtonsoft.Json.Linq.JArray(
                        from c in p.Category
                        select new Newtonsoft.Json.Linq.JValue(c)
                      )
                    )
                  )
                )
              )
            )
          )
        );

      return rss;
    }
    /*
      {
        "channel": {
          "title": "James Newton-King",
          "link": "http://james.newtonking.com",
          "description": "James Newton-King's blog.",
          "item": [{
            "title": "jewron",
            "description": "4546fds",
            "link": "http://www.baidu.com",
            "category": ["j", "h", "g", "j"]
          },
          {
            "title": "jofdsn",
            "description": "mdsfan",
            "link": "http://www.baidu.com",
            "category": ["6", "5", "4", "6"]
          },
          {
            "title": "jokjn",
            "description": "m3214an",
            "link": "http://www.baidu.com",
            "category": ["h", "g", "4", "2", "5"]
          },
          {
            "title": "jon",
            "description": "man",
            "link": "http://www.baidu.com",
            "category": ["g", "o", "o", "d", "m", "a", "n"]
          }]
        }
      }
     */

    public class Post
    {
      public string Title { get; set; }
      public string Description { get; set; }
      public string Link { get; set; }
      public string Category { get; set; }
    }
    private ListPost> GetPosts()
    {
      ListPost> listp = new ListPost>()
      {
        new Post{Title="jon",Description="man",Link="http://www.baidu.com",Category="goodman"},
        new Post{Title="jofdsn",Description="mdsfan",Link="http://www.baidu.com",Category="6546"},
        new Post{Title="jewron",Description="4546fds",Link="http://www.baidu.com",Category="jhgj"},
        new Post{Title="jokjn",Description="m3214an",Link="http://www.baidu.com",Category="hg425"}
      };
      return listp;
    }

    #endregion

    #region GetJArray
    /*
     * //example:=>
     * 
      Linq2Json l2j = new Linq2Json();
      Newtonsoft.Json.Linq.JArray jarray = l2j.GetJArray();
      return Newtonsoft.Json.JsonConvert.SerializeObject(jarray, Newtonsoft.Json.Formatting.Indented);
      //return jarray.ToString();
     */
    //Parsing a JSON Array from text 
    public Newtonsoft.Json.Linq.JArray GetJArray()
    {
      string json = @"[
               'Small',
               'Medium',
               'Large'
              ]";

      Newtonsoft.Json.Linq.JArray jarray = Newtonsoft.Json.Linq.JArray.Parse(json);
      return jarray;
    }

    //Creating JArray
    public Newtonsoft.Json.Linq.JArray GetJArray2()
    {
      Newtonsoft.Json.Linq.JArray array = new Newtonsoft.Json.Linq.JArray();
      Newtonsoft.Json.Linq.JValue text = new Newtonsoft.Json.Linq.JValue("Manual text");
      Newtonsoft.Json.Linq.JValue date = new Newtonsoft.Json.Linq.JValue(new DateTime(2000, 5, 23));
      //add to JArray
      array.Add(text);
      array.Add(date);

      return array;
    }

    #endregion

    //待續(xù)...

  }
}

測試效果:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testjson.aspx.cs" Inherits="microstore.testjson" %>

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

html xmlns="http://www.w3.org/1999/xhtml" >
head runat="server">
  title>/title>
  style type="text/css">
    body{ font-family:Arial,微軟雅黑; font-size:14px;}
    a{ text-decoration:none; color:#333;}
    a:hover{ text-decoration:none; color:#f00;}
  /style>
/head>
body>  
  form id="form1" runat="server">
    h3>序列化對象/h3>
    表現(xiàn)1:br />
    %=TestJsonSerialize()%>
    %=TestListJsonSerialize() %>
    表現(xiàn)2:br />
    %=TestListJsonSerialize2() %>
    hr />
    h3>反序列化對象/h3>
    p>單個對象/p>
    %=TestJsonDeserialize() %>
    p>多個對象/p>
    %=TestListJsonDeserialize() %>  
    p>反序列化成數(shù)據(jù)字典Dictionary/p>
    %=TestDeserialize2Dic() %>
    hr />  
    h3>自定義反序列化/h3>
    %=TestListCustomDeserialize()%>
    hr />
    h3>序列化輸出的忽略特性/h3>
    NullValueHandling特性忽略=>br />
    %=CommonSerialize() %>br />
    %=IgnoredSerialize()%>br />br />
    屬性標記忽略=>br />
    %=OutIncluded() %>br />
    %=OutIncluded2() %>
    hr />
    h3>Serializing Partial JSON Fragments/h3>
    %=SerializingJsonFragment() %>
    hr />
    h3>ShouldSerialize/h3>
    %=ShouldSerializeTest() %>br />
    %=JJJ() %>br />br />
    %=TestReadJsonFromFile() %>
  /form>
/body>
/html>

顯示:

序列化對象

表現(xiàn)1:

 

{ "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": null }

[ { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] }, { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] } ]

 表現(xiàn)2:

[ { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] }, { "Name": "Apple", "Expiry": "2014-05-04 02:08:58", "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] } ]

反序列化對象

單個對象

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large

多個對象

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large

•Apple
•2014-05-03 10:20:59
•3.99
•Small,Medium,Large

反序列化成數(shù)據(jù)字典Dictionary

Dictionary長度2
•key:key1,value:zhangsan
•key:key2,value:lisi

---------------------------------------------------------------

自定義反序列化

•當前List[x]對象類型:microstore.Employee
•FirstName:Maurice
•LastName:Moss
•BirthDate:1981-3-8 0:00:00
•Department:IT
•JobTitle:Support


•當前List[x]對象類型:microstore.Employee
•FirstName:Jen
•LastName:Barber
•BirthDate:1985-12-10 0:00:00
•Department:IT
•JobTitle:Manager

-------------------------------------------------------------

序列化輸出的忽略特性
NullValueHandling特性忽略=>
{ "Name": "Bad Boys III", "Description": "It's no Bad Boys", "Classification": null, "Studio": null, "ReleaseDate": null, "ReleaseCountries": null }
{ "Name": "Bad Boys III", "Description": "It's no Bad Boys" }

屬性標記忽略=>
{ "Model": "zhangsan", "Year": "2014-05-01T02:08:58.671875+08:00", "Features": [ "aaaa", "bbbb", "cccc" ] }
{ "Name": "zhangsan", "SalePrice": 3999.0 }
-----------------------------------------------------------------

Serializing Partial JSON Fragments
•Title:Paris Hilton - Wikipedia, the free encyclopedia
•Content: [1] In 2006, she released her debut album...
•Url:http://en.wikipedia.org/wiki/Paris_Hilton
•Title:Paris Hilton
•Content: Self: Zoolander. Socialite Paris Hilton...
•Url:http://www.imdb.com/name/nm0385296/

--------------------------------------------------------------------------------

ShouldSerialize
[ { "Name": "Joe Employee", "Manager": { "Name": "Mike Manager" } }, { "Name": "Mike Manager" } ]
{ "name": "Widget", "expiry": "2014-5-1 2:08:58", "price": 9.99, "sizes": [ "Small", "Medium", "Large" ] }

{ "channel": { "title": "James Newton-King", "link": "http://james.newtonking.com", "description": "James Newton-King's blog.", "item": [ { "title": "jewron", "description": "4546fds", "link": "http://www.baidu.com", "category": [ "j", "h", "g", "j" ] }, { "title": "jofdsn", "description": "mdsfan", "link": "http://www.baidu.com", "category": [ "6", "5", "4", "6" ] }, { "title": "jokjn", "description": "m3214an", "link": "http://www.baidu.com", "category": [ "h", "g", "4", "2", "5" ] }, { "title": "jon", "description": "man", "link": "http://www.baidu.com", "category": [ "g", "o", "o", "d", "m", "a", "n" ] } ] } }

以上這篇Json.net 常用使用小結(jié)(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 淺析JSONP技術(shù)原理及實現(xiàn)
  • JSONP原理及簡單實現(xiàn)

標簽:慶陽 聊城 臨夏 清遠 中衛(wèi) 海西 甘肅

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Json.net 常用使用小結(jié)(推薦)》,本文關(guān)鍵詞  Json.net,常用,使用,小結(jié),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Json.net 常用使用小結(jié)(推薦)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Json.net 常用使用小結(jié)(推薦)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    亚洲日本中文字幕区| 国产精品天干天干在观线| 中文文字幕文字幕高清| 午夜精品久久久久久久久久久| 在线观看福利片| 日日夜夜天天综合入口| 天天做天天爱综合| 国产在线视频卡一卡二| 成人免费视频一区二区| 国产欧美在线观看一区| 女人天堂av在线播放| 国产成人免费在线| 黄色精品一二区| 超碰97人人干| 精品视频在线免费看| 狠狠躁夜夜躁人人爽超碰91| 污视频在线看| 影音先锋久久久| 成年人网站91| 成人黄色免费短视频| 久久精品国产精品亚洲精品| 精品国产一区二区三区不卡| 免费在线视频欧美| 91精品国产乱码久久久久久蜜臀| 久久成人免费网| 欧美久久久网站| 亚洲午夜无码久久久久| 日韩av中文| 久久亚洲欧美国产精品乐播| 在线视频欧美性高潮| 性欧美freesex顶级少妇| 明星裸体视频一区二区| 日本高清视频一区二区| 精品一区二区三区五区六区七区| 99re6这里只有精品视频在线观看| jizzz18| 黄网站在线观看高清免费| 日本亚洲一区| 婷婷综合五月| 亚洲美女电影在线| 欧美巨乳在线观看| 97午夜影院| 欧美日韩一区二区区别是什么| 欧美激情一区二区三区高清视频| 老司机aⅴ在线精品导航| 国产精品香蕉在线观看| 538在线一区二区精品国产| 国产精品系列在线播放| 三级网站在线播放| 香蕉视频在线免费看| 国产精品一区二区av白丝下载| 亚洲无人区码一码二码三码| 羞羞视频网站在线免费观看| 日韩精品一区二区三区四区五区| 亚洲综合激情在线| 一区二区亚洲欧洲国产日韩| 天天操天天干天天插| 欧美日韩综合不卡| 午夜欧美性电影| 好男人官网在线观看| 综合激情网站| 鲁丝一区鲁丝二区鲁丝三区| xxxxwwww欧美| 二区三区偷拍浴室洗澡视频| 久久亚洲一区二区三区四区| 黄色的电影在线-骚虎影院-骚虎视频| 精品一区二区三区电影| 亚洲福利一区| 伊人成人在线| 日本国产一区二区| 亚洲最大的成人网| 狠狠干狠狠久久| 亚洲第一成肉网| xxx欧美精品| 国产精品视频一区二区三区不卡| av激情亚洲男人天堂| 香蕉成人app免费看片| 国产精品嫩草99av在线| 欧美系列一区二区| 午夜精品久久久久久久99老熟妇| 性做久久久久久久免费看| 国产精品欧美激情| 一区二区在线观看免费视频播放| 国产在线不卡精品| 调教驯服丰满美艳麻麻在线视频| 国产福利电影一区二区三区| 一区二区欧美久久| xxxx在线视频| 久久久久久在线观看| 亚洲中文字幕无码av永久| 亚洲人体av| 亚洲欧洲午夜一线一品| 欧美一区二区三区久久综| 日本午夜一本久久久综合| 亚洲欧美日韩高清| 99免费精品在线观看| 97精品人妻一区二区三区在线| 美日韩一区二区| 国产伦精品一区二区三区视频网站| 日韩三级中文字幕| 色视频精品视频在线观看| 国产超级va在线视频| 欧洲在线视频| 国产麻豆精品在线观看| 日本高清视频免费看| 中国一级特黄录像播放| 国产精品久久久久久av公交车| 精品在线小视频| 老鸭窝av在线| 国产免费福利视频| 亚洲福利在线观看视频| 四虎国产精品免费久久| 国产精品第五页| 性活交片大全免费看| 亚洲国产黄色片| 日本欧美韩国| 欧美成人三级在线视频| 天天操天天射天天插| 国产成人精品aa毛片| 大胆高清日本a视频| 欧美日韩激情一区二区三区| 91精品久久久久久蜜桃| b站大片免费直播| 日韩视频一区二区三区在线播放| 午夜久久电影网| 九九热青青草| 精品少妇一区二区三区| 精品影片一区二区入口| 久久91亚洲精品中文字幕| 四虎成人精品在永久在线观看| 老司机精品视频网站| 欧美日韩国产在线看| 国产精品免费一区二区三区| 亚洲国产精品精华液2区45| 尤物在线观看视频| 色偷偷网友自拍| 91精品视频在线看| 视频一区视频二区视频三区视频四区国产| 国产自产女人91一区在线观看| 在线免费91| 亚洲综合一区中| 郴州新闻综合频道在线直播| 欧美特黄一级视频| 四虎久久免费| 日本三级久久| 手机毛片在线观看| 99久久亚洲国产日韩美女| 99热国产在线中文| 在线视频播放大全| 国产日韩在线视频| 成人美女免费网站视频| 欧洲一区二区三区精品| 国产一级一区二区| av网站网址在线观看| 自由的xxxx在线视频| 最新中文字幕视频| 丝袜在线视频| 国产亚洲精品av| av免费播放网站| 亚洲日本va在线观看| 欧美午夜电影在线观看| 中国丰满人妻videoshd| 国产av无码专区亚洲av| www国产无套内射com| 色18美女社区| 亚洲91网站| 欧美最猛黑人xxxx黑人猛交黄| 91制片厂免费观看| 欧美**vk| 国产另类第一区| 日本精品免费观看高清观看| 天堂av资源在线观看| 久久久久久久欧美精品| 欧美三级午夜理伦三级富婆| 99视频一区二区三区| 先锋av影院| 粉嫩av一区二区三区天美传媒| 狠狠干一区二区| 看片网站在线观看| 一卡二卡三卡在线| 欧美天堂在线| 国产亚洲精品激情久久| 91丨porny丨在线中文| 亚洲综合小说图片| 成人性爱视频在线观看| a级片在线播放| 狠狠操综合网| 国产精品国产三级国产专业不| 国产日产欧美a一级在线| 欧美精品日韩少妇| 色精品一区二区三区| 国产精品三区www17con| 麻豆精品蜜桃| 成人性生交大片免费观看嘿嘿视频| 亚洲高清久久久久久| 日韩av在线天堂网| 成人av无码一区二区三区| 啊v视频在线一区二区三区| 亚洲第一区第二区| 欧美日韩国产观看视频| 日韩一级欧洲| 国产精品国产三级国产aⅴ中文| 男男视频在线观看网站| 国产精品嫩草影院av蜜臀| 亚洲欧洲二区| 欧美日韩一区二区三区在线电影| 青青在线免费视频| 国产高清日韩| 日韩美女视频免费在线观看| 制服师生第一页| аⅴ资源天堂资源库在线| 91沈先生在线观看| 国产无精乱码一区二区三区| 五月天婷婷在线播放| 欧美色图亚洲天堂| 中文字幕av影视| 国产在线视频不卡二| 写真福利精品福利在线观看| 国产欧美日韩激情| 国产高潮失禁喷水爽到抽搐| 日韩视频第一页| 韩国日本一区二区三区| 亚洲熟妇av乱码在线观看| 久久久午夜视频| av在线不卡一区| 国产91精品在线播放| 国产真实有声精品录音| 黄视频免费在线看| 99久久亚洲精品蜜臀| 国产一区高清在线| 精品夜夜嗨av一区二区三区| 麻豆91在线看| 欧美日韩在线综合| 99久久精品国产亚洲| 欧美不卡激情三级在线观看| 欧美在线观看在线观看| 国内精品福利| 欧美天堂在线观看| 一色屋精品亚洲香蕉网站| 久久精品盗摄| 777午夜精品视频在线播放| 欧美jjzz| 韩国三级hd两男一女| 久久久久久视频| 噜噜噜噜噜在线视频| 国产精品秘入口| 在线免费观看日本欧美| 高清在线观看免费韩剧| 有码中文亚洲精品| 加勒比av一区二区| a看欧美黄色女同性恋| 国产精品变态另类虐交| 国语自产精品视频在线看抢先版图片| 亚洲欧美激情在线观看| av中文字幕一区二区三区| 国产精品久久久久久久久男| 精品粉嫩超白一线天av| 亚洲男人的天堂在线aⅴ视频| 亚洲高清在线视频| 久久久久久久伊人| 91极品视频在线观看| 久热免费在线视频| 亚洲美女免费精品视频在线观看| 日韩av高清在线播放| 免费在线性爱视频| 福利视频理论电影| 亚洲bt天天射| 中文字幕视频网| 成人免费视频网站入口| 久久99精品久久久久久青青91| 91精品久久| 疯狂做受xxxx欧美肥白少妇| 欧美日本国产在线| 视频在线观看一区| 自由的xxxx在线视频| 99在线免费视频| 午夜精品久久久内射近拍高清| 粉嫩在线一区二区三区视频| 91九色视频在线| 国产综合精品久久久久成人av| 欧美精品a∨在线观看不卡| 久久er热在这里只有精品66| 国产精品一区二区免费| 国产制服丝袜一区| 91视频青青草| 国产精品伦子伦| 国产喷白浆一区二区三区| 日本中文字幕中出在线| 国产chinese精品一区二区| 精品久久国产视频| wwwxxx亚洲| 9999精品视频| 日韩少妇裸体做爰视频| 日韩精品一区二区三区免费观影| 国产美女视频一区二区三区| 二级片在线观看| 亚洲区综合中文字幕日日| 日本天堂网在线| 日韩欧美国产三级电影视频| 欧美一二三四区在线| 992kp快乐看片永久免费网址| 亚洲国产精品视频在线| 久久久久久一区二区三区| 黄网站app在线观看大全免费视频| yiren22亚洲综合伊人22| 日韩美女视频一区二区| 午夜精品久久久久久久蜜桃app| 亚洲激情成人| 超碰人人干人人| 精品久久香蕉国产线看观看亚洲| 国产在线欧美| av软件在线观看| 黄视频网站免费看| 在线观看国产视频| 91在线观看免费高清| 日本三级片在线观看| 国内一区在线| 免费午夜一级| 欧美一区二区福利| 超碰国产在线观看| 国产精品福利网站| 日韩中文首页| 玖玖精品在线| 视频二区欧美| 视频一区二区三区在线看免费看| 日韩网站在线看片你懂的| 成年人在线免费看片| 中文字幕永久免费视频|