Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t使用JSON.NET正确序列化JSON_C#_Json_Xml_Serialization_Json.net - Fatal编程技术网

C# Can';t使用JSON.NET正确序列化JSON

C# Can';t使用JSON.NET正确序列化JSON,c#,json,xml,serialization,json.net,C#,Json,Xml,Serialization,Json.net,我使用JSON.net将非常复杂的XML转换为JSON,并将其反序列化为C# string text=wait blockBlob2.DownloadTextAsync(); XmlDocument doc=新的XmlDocument(); doc.LoadXml(文本); 字符串json=Newtonsoft.json.JsonConvert.SerializeXmlNode(doc); RootObject x=JsonConvert.DeserializeObject(json); 字符串

我使用JSON.net将非常复杂的XML转换为JSON,并将其反序列化为C#

string text=wait blockBlob2.DownloadTextAsync();
XmlDocument doc=新的XmlDocument();
doc.LoadXml(文本);
字符串json=Newtonsoft.json.JsonConvert.SerializeXmlNode(doc);
RootObject x=JsonConvert.DeserializeObject(json);
字符串输出=JsonConvert.SerializeObject(x);
从XML转换后,JSON中出现了“@”符号,这是我面临的问题。当我尝试序列化它时,所有结构都正常工作,但字段的内容变为空。

然后序列化对象无法正常工作:

RootObjectClass:

    /* 
 Licensed under the Apache License, Version 2.0

 http://www.apache.org/licenses/LICENSE-2.0
 */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace WorkerRole1
{

    public class ItineraryOption
    {
        public string ODRef { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string Date { get; set; }
        public string SeatsAvailable { get; set; }
        public string TravelTime { get; set; }
        public string ItineraryRef { get; set; }
        public object FlightSegment { get; set; }
    }

    public class ItineraryOptions
    {
        public List<ItineraryOption> ItineraryOption { get; set; }
    }

    public class BookingGuidelines
    {
        public string RussianNamesSupported { get; set; }
    }

    public class DeepLink
    {
        public string DeviceType { get; set; }
        //public string __invalid_name__#cdata-section { get; set; }
   // public string __invalid_name__#text { get; set; }
}

    public class DeepLinks
    {
        public List<DeepLink> DeepLink { get; set; }
    }

    public class ShopOption
    {
        public string OptionRef { get; set; }
        public string Currency { get; set; }
        public string Total { get; set; }
        public string Airlines { get; set; }
        public ItineraryOptions ItineraryOptions { get; set; }
        public BookingGuidelines BookingGuidelines { get; set; }
        public DeepLinks DeepLinks { get; set; }
    }

    public class ShopOptions
    {
        public List<ShopOption> ShopOption { get; set; }
    }

    public class SIGAirShopRS
    {
        public string CustomerID { get; set; }
        public string SessionID { get; set; }
        public string ProcessingTime { get; set; }
        public string SIGVersion { get; set; }
        public string SchemaVersion { get; set; }
        public string BuildDate { get; set; }
        public string Result { get; set; }
        public ShopOptions ShopOptions { get; set; }
    }

    public class SIGResponse
    {
        public string xmlns { get; set; }
        public SIGAirShopRS SIG_AirShopRS { get; set; }
    }

    public class RootObject
    {
        public SIGResponse SIG_Response { get; set; }
    }

}
/*
根据Apache许可证2.0版获得许可
http://www.apache.org/licenses/LICENSE-2.0
*/
使用制度;
使用System.Xml.Serialization;
使用System.Collections.Generic;
命名空间工作错误1
{
公务舱行程选择
{
公共字符串ODRef{get;set;}
来自{get;set;}的公共字符串
{get;set;}的公共字符串
公共字符串日期{get;set;}
公共字符串SeatsAvailable{get;set;}
公共字符串旅行时间{get;set;}
公共字符串巡回引用{get;set;}
公共对象FlightSegment{get;set;}
}
公务舱行程选择
{
公共列表行程选项{get;set;}
}
公共课预订指南
{
支持的公共字符串RussianaMess{get;set;}
}
公共类深度链接
{
公共字符串设备类型{get;set;}
//公共字符串uu无效uu名称uu35; cdata节{get;set;}
//公共字符串uu无效u名称uu文本{get;set;}
}
公共类深度链接
{
公共列表深度链接{get;set;}
}
公共类购物选择
{
公共字符串OptionRef{get;set;}
公共字符串货币{get;set;}
公共字符串总计{get;set;}
公共字符串{get;set;}
公共行程选项行程选项{get;set;}
公共预订指南预订指南{get;set;}
公共深度链接深度链接{get;set;}
}
公共类购物选择
{
公共列表选项{get;set;}
}
公共级飞艇
{
公共字符串CustomerID{get;set;}
公共字符串SessionID{get;set;}
公共字符串处理时间{get;set;}
公共字符串版本{get;set;}
公共字符串SchemaVersion{get;set;}
公共字符串BuildDate{get;set;}
公共字符串结果{get;set;}
公共商店选项商店选项{get;set;}
}
公共类SIG响应
{
公共字符串xmlns{get;set;}
公共标志飞机场标志飞机场{get;set;}
}
公共类根对象
{
公共SIG响应SIG_响应{get;set;}
}
}
XML示例:

我建议用
JsonPropertyAttribute
标记
RootObject
类中的属性,并将Json中自动生成的“@”属性名称显式设置为本地类属性名称,例如:

public string xmlns {get;set;}
…变成

[JsonProperty("@xmlns")]
public string xmlns {get;set;}
根据NewtonSoft JSON C#反序列化示例,普通POCO对象不会为JSON中的属性生成@style名称,但我怀疑这是因为您正在从XML转换(
xmlns
例如,通常是XMLat属性)


另外,
Result
属性序列化很好,您会注意到它是唯一一个没有前缀“@”的JSON属性。

添加您的RootObject类和示例数据为什么要转到JSON,然后转到C#对象而不是直接指向对象的XML?问题是由“@”符号引起的吗?如果您不确定是否应该再次检查该字符,然后可能“转义”该字符。有必要进行转换以备将来使用。添加了classfound解决方法,但不确定这是最佳方法:XmlDocument doc=new json=json.Replace(“@”和“”);谢谢你,伙计,我去查一下。我找到了另一种解决方法-json=json.Replace(“@”和“”);但按我的方式做是个坏主意(没问题。如果解决了您的问题,请将我的答案标记为正确。:)而且,我认为,如果不打算将JSON恢复为XML,那就太糟糕了。否则,将减少所有这些属性的维护!
[JsonProperty("@xmlns")]
public string xmlns {get;set;}