json.help

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涉及到返回日期的,就可以让控制器继承我们自定义的这个类,由此解决日期问题。




*/

  

你可能感兴趣的:(json)