开发者

How to Serialize XML to a JSON object with Json.NET

开发者 https://www.devze.com 2023-03-23 19:34 出处:网络
I can serialize XML to a JSON string like this: var xml = new XmlDocument(); xml.LoadXml(\"<person><name>John</name></person>\");

I can serialize XML to a JSON string like this:

var xml = new XmlDocument();
xml.LoadXml("<person><name>John</name></person>");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.None);
Response.ContentType = "application/json";
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(jsonString));

That would give me:

"{\"person\开发者_运维问答":{\"name\":\"John\"}}"

But how can I serialize it to a JSON object? Like this:

{"person":{"name":"John"}}


Sometimes we just want to make it harder than it is ...

var xml = new XmlDocument();
xml.LoadXml("<person><name>John</name></person>");
Response.ContentType = "application/json";
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(xml));

What I did wrong was to serialize the XML into a string and then serialize it again.


when you will access data then / automatically does not show . I am accessing in HTML5 help of AJAX post . Result is showing

in C# result is showing that "{\"person\":{\"name\":\"John\"}}"

But in HTML5 , it is working fine {"person":{"name":"John"}}

0

精彩评论

暂无评论...
验证码 换一张
取 消