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

      

    作者:Bober Song
    出处:http://bober.cnblogs.com/
    CARE健康网: http://www.aicareyou.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Simple Form Fading
    CResizableFormView
    User-Defined Multi-Lingual Applications
    Automatic Resizing of a FormView Based Application When the User Toggles the Control Bars On or Off
    Save Zip File in Access Database With File Encryption in C#
    continue
    break
    declare/typeset
    bg/fg/jobs
    .
  • 原文地址:https://www.cnblogs.com/bober/p/3023891.html
Copyright © 2011-2022 走看看