我的JSON到XML代码存在问题。它没有将值分配给Object,我无法弄清楚原因。请让我知道我做错了什么。
我的C#代码:
using Newtonsoft.Json;
using System.Xml;
namespace JSONTest
{
public class Program
{
static void Main(string[] args)
{
string fileName = "C:\\Code\\JSONTest\\data\\response.xml";
// Convert XML Data into JSON Data
XmlDocument xmlFile = new XmlDocument();
xmlFile.Load(fileName);
string jsonData = JsonConvert.SerializeXmlNode(xmlFile);
// Convert JSON Data into Object
RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonData);
var data = root.RESPONSE_GROUP;
Console.ReadLine();
}
}
public class RootObject
{
public RESPONSEGROUP RESPONSE_GROUP { get; set; }
}
public class RESPONSEGROUP
{
public string MISMOVersionID { get; set; }
public object RESPONDING_PARTY { get; set; }
public object RESPOND_TO_PARTY { get; set; }
public RESPONSE RESPONSE { get; set; }
}
public class RESPONSE
{
public string ResponseDateTime { get; set; }
public KEY KEY { get; set; }
public STATUS STATUS { get; set; }
}
public class KEY
{
public string _Name { get; set; }
public string _Value { get; set; }
}
public class STATUS
{
public string _Code { get; set; }
public string _Condition { get; set; }
public string _Description { get; set; }
public string _Name { get; set; }
}
}
XML
<RESPONSE_GROUP MISMOVersionID="2.4">
<RESPONDING_PARTY/>
<RESPOND_TO_PARTY/>
<RESPONSE ResponseDateTime="2015-02-19T10:32:11-06:00">
<KEY _Name="LOSClientID" _Value="3000799866"/>
<STATUS _Code="S0010" _Condition="Success" _Description="TEST DESC" _Name="Complete"/>
</RESPONSE>
</RESPONSE_GROUP>
我的&#34; JSONData&#34;字符串:
{"RESPONSE_GROUP":{"@MISMOVersionID":"2.4","RESPONDING_PARTY":null,"RESPOND_TO_PARTY":null,"RESPONSE":{"@ResponseDateTime":"2015-02-19T10:32:11-06:00","KEY":{"@_Name":"LOSClientID","@_Value":"3000799866"},"STATUS":{"@_Code":"S0010","@_Condition":"Success","@_Description":"THIS IS THE DESCRIPTION.","@_Name":"Complete"}}}}
root.RESPONSE_GROUP.MISMOVersionID
的值为NULL
以及应填充的任何其他值。我知道我在这里做错了什么,但我无法弄清楚它是什么。
请帮忙!提前谢谢。
答案 0 :(得分:3)
问题是您的JSON在某些属性名称前面包含@
个符号。例如:
"@MISMOVersionID":"2.4"
这里有两个选项:
"@MISMOVersionID":"2.4"
使用JsonPropertyAttribute
告诉Json.NET在JSON中需要哪个属性名称,例如
[JsonProperty("@MISMOVersionID")]
public string MISMOVersionID { get; set; }