JSON转换为XML [英] Converting JSON to XML

查看:1921
本文介绍了JSON转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图JSON输出转换成XML。不幸的是我得到这个错误:

I trying to convert JSON output into XML. Unfortunately I get this error:

JSON根对象有多个属性。根对象必须以创建一个有效的XML文档有一个属性。考虑specifing一个DeserializeRootElementName。

JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName.

这是我达到现在创建。

string url = string.Format("https://graph.facebook.com/{0}?fields=posts.fields(message)&access_token={1}", user_name, access_token);

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    jsonOutput = reader.ReadToEnd();
    Console.WriteLine("THIS IS JSON OUTPUT: " + jsonOutput);
}
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonOutput);
Console.WriteLine(doc);



这是我的JSON输出:

And this is my JSON output:

{"id":"108013515952807","posts":{"data":[{"id":"108013515952807_470186843068804","created_time":"2013-05-14T20:43:28+0000"},{"message":"TEKST","id":"108013515952807_470178529736302","created_time":"2013-05-14T20:22:07+0000"}

我该如何解决这个问题呢?

How can I solve this problem?

推荐答案

尽管在这个问题只要您的JSON是不完整的,你必须在顶层多个属性由异常指示。你必须定义根它来获得有效的XML:

Despite the fact your JSON provided in the question is not complete, you have multiple properties at the top level as indicated by the exception. You have to define the root for it to get valid XML:

var doc = JsonConvert.DeserializeXmlNode(jsonOutput, "root");



编辑:为了打印出你的XML缩进可以使用的XDocument 从类 System.Xml.Linq的命名空间: XDocument.Parse(doc.InnerXml)

In order to print out your XML with indentation you can use XDocument class from System.Xml.Linq namespace: XDocument.Parse(doc.InnerXml).

这篇关于JSON转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆