[英]Can't serialize JSON properly using 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中出現“ @”符號的問題。 當我嘗試對其進行序列化時,所有結構都可以使用,但是字段的內容為空。
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
我建議使用JsonPropertyAttribute
在RootObject
類中標記屬性, RootObject
將Json中自動生成的“ @”屬性名稱的名稱顯式設置為您本地類的屬性名稱,例如:
public string xmlns {get;set;}
..becomes ..
[JsonProperty("@xmlns")]
public string xmlns {get;set;}
按照NewtonSoft JSON C#反序列化的樣本,一個正常的POCO對象不會產生對JSON屬性@風格的名字,但我懷疑這是因為你是從XML轉換( xmlns
例如在致敬的XML正常)。
此外, Result
屬性可以很好地序列化,您會注意到它是唯一不帶“ @”前綴的JSON屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.