如何将XML反序列化为json和json转换为c#类? [英] How do Deserialize XML to json and json to c# classes?

查看:122
本文介绍了如何将XML反序列化为json和json转换为c#类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   // This is my xml data I am trying to map this to my .net classes. By converting XML to json and json back to C# classes.

       string xml = @"  <ReportTemplate>
      <Page Id='1' LayoutId='1'>
        <Section Id='1'>
          <Body>TEststindfgdfgf</Body>
        </Section>`enter code here`
        <Section Id='2'>
          <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News' ></Content>
        </Section>
        <Section Id='3'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
      </Page>
      <Page Id='2' LayoutId='1'>
        <Section Id='1'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
          <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
        <Section Id='2'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
        <Section Id='3'>
          <Body>dfgdgggf;</Body>
        </Section>
      </Page>
    </ReportTemplate>";


        public class ReportTemplate
        {
              public List<Page> Page { get; set; }
        }
        public class Page
        {
            public string Id { get; set; }
            public string LayoutId { get; set; }
            public List<Section> Section { get; set; }
        }

        public class Section
        {
            public string Id { get; set; }
            public string Body { get; set; }
            public List<Content> Content { get; set; }
        }
        public class Content
        {
            public string Id { get; set; }
            public string Type { get; set; }

        }



var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
var json = JsonConvert.SerializeXmlNode(xmldoc,Newtonsoft.Json.Formatting.Indented, true);
var obj = JsonConvert.DeserializeObject<ReportTemplate>(Regex.Replace(json, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase));

//这是我得到的错误

Newtonsoft.Json.JsonSerializationException:'无法反序列化 当前的JSON对象(例如{"name":"value"}) 'System.Collections.Generic.List`1 [LintoXML.Program + Content]'因为 该类型需要JSON数组(例如[1,2,3])进行反序列化 正确地.要解决此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型,以使其成为常规 .NET类型(例如,不是整数之类的原始类型,不是集合 可以从JSON反序列化的类型(如数组或列表) 目的.也可以将JsonObjectAttribute添加到类型中以强制它 从JSON对象反序列化.小路 页面[0].部分[2] .Content.Id",第27行,位置17.'

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LintoXML.Program+Content]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Page[0].Section[2].Content.Id', line 27, position 17.'

推荐答案

将XML序列化为JSON之后,复制JSON并通过引用

Once you have serialized the XML into JSON, copy the JSON and generate the classes for it by referring this answer. Here are the classes:

public class Rootobject
{
    public Reporttemplate ReportTemplate { get; set; }
}

public class Reporttemplate
{
    public Page[] Page { get; set; }
}

public class Page
{
    public string Id { get; set; }
    public string LayoutId { get; set; }
    public Section[] Section { get; set; }
    public string text { get; set; }
}

public class Section
{
    public string Id { get; set; }
    public string Body { get; set; }
    public object Content { get; set; }
}

然后将JSON反序列化为这些类:

Then deserialize the JSON into those classes:

var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
var fromXml = JsonConvert.SerializeXmlNode(xmldoc);
var fromJson = JsonConvert.DeserializeObject<Rootobject>(fromXml);

这篇关于如何将XML反序列化为json和json转换为c#类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆