json.help

來源:互聯網
上載者:User
using System;using System.Net;using System.Collections.Generic;using System.IO;using System.Xml;namespace CiCeng{public class CiCengjson{public object XmlToJson(){var xml = File.ReadAllText(@"D:\cnblogs.xml");//該xml為部落格園隨筆備份檔案XmlDocument doc = new XmlDocument();doc.LoadXml(xml);//擷取rss節點下的內容var channelXml = doc.SelectSingleNode("rss").InnerXml;//進一步細化xml格式,內容僅為rss節點下的內容doc.LoadXml(channelXml);//將xml序列化成json,並且去掉根節點var json = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true);}public object JsonToObject(object json){var channel = JsonConvert.DeserializeObject<Channel>(json);return channel;}}public class Channel{public string title { get; set; }public string link { get; set; }public string description { get; set; }public string language { get; set; }public string lastBuildDate { get; set; }public string pubDate { get; set; }public string ttl { get; set; }public List<Channel_Item> item { get; set; }}public class Channel_Item{public string title { get; set; }public string link { get; set; }public string author { get; set; }public string pubDate { get; set; }public string guid { get; set; }public Item_Description description { get; set; }}public class Item_Description{//預設以變數名稱作為json序列化的節點,由於該節點內容不符合變數定義規範,則顯示指定即可[JsonProperty("#cdata-section")]public string content { get; set; }}}/*//http://www.cnblogs.com/chongsha/archive/2013/04/14/3019990.html自訂了一個類,繼承JSONRESULT類並將上面的方法從寫 ,用newton.json替換javascriptSerializer替換代碼如下:public override void ExecuteResult(ControllerContext context){if (this.Data != null){IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";response.Write(JsonConvert.SerializeObject(this.Data, Newtonsoft.Json.Formatting.Indented, timeFormat, x););}}重新定義一個Controller,命名為 JsonController,且設定為抽象類別。然後重寫方法:protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior){return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };}將 JsonResult改為我們繼承JSONResult並重寫了ExecuteResult方法的類,就大工告成了!//http://www.cnblogs.com/codealone/archive/2013/04/14/3020161.html以後只要JsonResult涉及到返回日期的,就可以讓控制器繼承我們自訂的這個類,由此解決日期問題。*/

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.