C#Newton.Json反序列化
我想读取xml并转换json,然后将该json转换为C#对象。 我知道我可以使用linq初始化对象。 但我想要实现的是读取xml并将其转换为json,然后将转换后的字符串反序列化为对象。我无法正确初始化对象。 我错过了什么C#Newton.Json反序列化,c#,json,xml,serialization,C#,Json,Xml,Serialization,我想读取xml并转换json,然后将该json转换为C#对象。 我知道我可以使用linq初始化对象。 但我想要实现的是读取xml并将其转换为json,然后将转换后的字符串反序列化为对象。我无法正确初始化对象。 我错过了什么 public class Cash { public string Amount { get; set; } } public class POSLog { public string MajorVersion { get; set; } publi
public class Cash
{
public string Amount { get; set; }
}
public class POSLog
{
public string MajorVersion { get; set; }
public string MinorVersion { get; set; }
public string FixVersionive { get; set; }
public Cash Cashx { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
try
{
XmlDocument xml = new XmlDocument();
xml.LoadXml("<POSLog MajorVersion=\"6\" MinorVersion=\"0\" FixVersion=\"0\"><Cash Amount = \"100\"></Cash></POSLog>");
string json = JsonConvert.SerializeObject(xml.InnerXml);
POSLog deserializedProduct = JsonConvert.DeserializeObject<POSLog>(json);
Console.WriteLine("Major Version" + deserializedProduct.MajorVersion);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
公共类现金
{
公共字符串金额{get;set;}
}
公共类POSLog
{
公共字符串主版本{get;set;}
公共字符串MinorVersion{get;set;}
公共字符串固定版本{get;set;}
公共现金现金x{get;set;}
}
公共课程
{
公共静态void Main(字符串[]args)
{
尝试
{
XmlDocument xml=新的XmlDocument();
xml.LoadXml(“”);
字符串json=JsonConvert.SerializeObject(xml.InnerXml);
POSLog deserializedProduct=JsonConvert.DeserializeObject(json);
Console.WriteLine(“主版本”+反序列化的产品.MajorVersion);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
}
您不能将包含xml
的字符串序列化为json
并期望它反序列化为POSLog
。在继续之前,将xml反序列化为POSLog
。问题在于反序列化错误
您必须首先从XML反序列化到POSLog,然后将POSLog序列化为json
void Main()
{
try
{
string xmlDoc = "<POSLog MajorVersion=\"6\" MinorVersion=\"0\" FixVersion=\"0\"><Cash Amount = \"100\"></Cash></POSLog>";
XmlSerializer xser = new XmlSerializer(typeof(POSLog));
POSLog fromXml = xser.Deserialize(new StringReader(xmlDoc)) as POSLog;
string json = JsonConvert.SerializeObject(fromXml);
POSLog fromJson = JsonConvert.DeserializeObject<POSLog>(json);
Console.WriteLine("MajorVersion=" + fromJson.MajorVersion);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// Define other methods and classes here
public class Cash
{
public string Amount { get; set; }
}
public class POSLog
{
[XmlAttribute]
public string MajorVersion { get; set; }
[XmlAttribute]
public string MinorVersion { get; set; }
[XmlAttribute]
public string FixVersionive { get; set; }
public Cash Cashx { get; set; }
}
void Main()
{
尝试
{
字符串xmlDoc=”“;
XmlSerializer xser=新的XmlSerializer(typeof(POSLog));
POSLog fromXml=xser.Deserialize(新的StringReader(xmlDoc))为POSLog;
字符串json=JsonConvert.SerializeObject(fromXml);
POSLog fromJson=JsonConvert.DeserializeObject(json);
Console.WriteLine(“MajorVersion=“+fromJson.MajorVersion”);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
//在此处定义其他方法和类
公共类现金
{
公共字符串金额{get;set;}
}
公共类POSLog
{
[XmlAttribute]
公共字符串主版本{get;set;}
[XmlAttribute]
公共字符串MinorVersion{get;set;}
[XmlAttribute]
公共字符串固定版本{get;set;}
公共现金现金x{get;set;}
}
根据文档:
转换规则
- 要素保持不变
- 属性的前缀为@,并且应该位于对象的开头。
- 单子文本节点是直接针对元素的值,否则可通过#text访问它们
- XML声明和处理指令的前缀为
- 字符数据、注释、空格和有效空格节点分别通过#cdata节、#注释、#空格和#有效空格进行访问
- 同一级别上具有相同名称的多个节点被分组到一个数组中
- 空元素为空
如果从JSON创建的XML与您想要的不匹配,那么您需要手动转换它。最好的方法是将JSON加载到LINQtoJSON对象(如JObject或JArray)中,然后使用LINQ创建XDocument
考虑到这一点,为解决您的问题而创建的以下单元测试将通过
[TestClass]
public class JsonToXmlTests : MiscUnitTests {
[TestMethod]
public void Xml_Should_Convert_To_JSON_And_Object() {
string xml = "<POSLog MajorVersion=\"6\" MinorVersion=\"0\" FixVersion=\"0\"><Cash Amount = \"100\"></Cash></POSLog>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.None, true);
//Attributes are prefixed with an @ and should be at the start of the object.
jsonText = jsonText.Replace("\"@", "\"");
POSLog actual = JsonConvert.DeserializeObject<POSLog>(jsonText);
actual.Should().NotBeNull();
actual.MajorVersion.Should().Be("6");
actual.MinorVersion.Should().Be("0");
actual.FixVersion.Should().Be("0");
actual.Cash.Should().NotBeNull();
actual.Cash.Amount.Should().Be("100");
}
public class Cash {
public string Amount { get; set; }
}
public class POSLog {
public string MajorVersion { get; set; }
public string MinorVersion { get; set; }
public string FixVersion { get; set; }
public Cash Cash { get; set; }
}
}
[TestClass]
公共类JsonToXmlTests:MiscUnitTests{
[测试方法]
public void Xml_应_将_转换为_JSON_和_Object(){
字符串xml=”“;
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xml);
字符串jsonText=JsonConvert.SerializeXmlNode(doc,Newtonsoft.Json.Formatting.None,true);
//属性的前缀为@,并且应该位于对象的开头。
jsonText=jsonText.Replace(“\”@“,“\”);
POSLog-actual=JsonConvert.DeserializeObject(jsonText);
实际的.Should().NotBeNull();
实际.MajorVersion.Should()为(“6”);
实际.MinorVersion.Should()为(“0”);
实际.FixVersion.Should()为(“0”);
实际.现金.应().NotBeNull();
实际现金金额应为(“100”);
}
公共类现金{
公共字符串金额{get;set;}
}
公共类POSLog{
公共字符串主版本{get;set;}
公共字符串MinorVersion{get;set;}
公共字符串固定版本{get;set;}
公共现金{get;set;}
}
}
为什么不直接从xml转换为对象?它是否正确地序列化了json?在POSLog
中,变量的命名也为FixVersionive
,与xml中的命名不匹配。