无法使用JSON.NET正确序列化JSON

时间:2015-10-01 15:16:13

标签: c# json xml serialization json.net

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

string text = await blockBlob2.DownloadTextAsync();

XmlDocument doc = new XmlDocument();
doc.LoadXml(text);
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

RootObject x = JsonConvert.DeserializeObject<RootObject>(json);
string output = JsonConvert.SerializeObject(x);

我遇到了从XML转换后在JSON中出现的“@”符号问题。当我尝试序列化它时,所有结构都有效,但字段的内容变为空。 @ added after automatically, I don't know why

然后SerializeObject无法正常工作:enter image description here

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; }
    }

}

XML示例: https://drive.google.com/file/d/0B3CEquA-V15lamN5UVRWeDdzR2M/view?usp=sharing

1 个答案:

答案 0 :(得分:0)

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

public string xmlns {get;set;}

.. ..变为

[JsonProperty("@xmlns")]
public string xmlns {get;set;}

根据NewtonSoft JSON C#反序列化样本,普通的POCO对象不会为JSON中的属性生成@ style名称,但我怀疑这是因为你是从XML转换的(xmlns例如是XML 正常致敬。)

同样Result属性序列化很好,你会注意到它是唯一没有前缀为“@”的JSON属性。