2013-05-15 13 views
14

Próbuję przekonwertować wyjście JSON do XML. Niestety pojawia się ten błąd:Konwersja JSON do XML

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.

To jest to, co do tej pory stworzyłem.

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); 

A to moje wyjście JSON:

{"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"} 

Jak mogę rozwiązać ten problem?

Odpowiedz

25

Pomimo faktu Twój JSON przewidzianego w pytaniu nie jest kompletny, masz wiele właściwości na najwyższym poziomie jak wskazano wyjątku. Trzeba zdefiniować pierwiastek na to, aby uzyskać poprawny XML:

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

EDIT: Aby wydrukować swój XML z wcięciem można użyć XDocument klasę z System.Xml.Linq nazw: XDocument.Parse(doc.InnerXml).

+0

masz pomysł jak wyświetlić XML? Dostaję ten plik System.Xml.XmlDocument :( – lukso

+1

Możesz użyć klasy 'XDocument' z przestrzeni nazw' System.Xml.Linq' aby wydrukować twój XML z wcięciem: 'XDocument.Parse (doc.InnerXml)'. – jwaliszko

0

Udostępniony JSON jest nieprawidłowy, należy przejść przez http://jsonformatter.curiousconcept.com/ i najpierw sprawdzić poprawność JSON.

Yourt JSON powinna wyglądać następująco:

{ 
    "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" 
     } 
     ] 
    } 
} 
1

DeserializeXmlNode zwraca XDcument. W razie potrzeby XNode użyj FirstNode.

//string jsonOutput="{"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"}"; 
var myelement= JsonConvert.DeserializeXmlNode(jsonOutput, "myelement").FirstNode; 
+0

Zła metoda. Użyj DeserializeXNode (...) dla XDocument – Fried

4

Pomyślałem, że warto połączyć się z Documentation for turning xml to json and the other way around.

Chłopaki mają rację ..

// To convert an XML node contained in string xml into a JSON string 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xml); 
string jsonText = JsonConvert.SerializeXmlNode(doc); 

// To convert JSON text contained in string json into an XML node 
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); 
2

Można zrobić JSON do XML również przy użyciu .NET Framework (System.Runtime.Serialization.Json):

private static XDocument JsonToXml(string jsonString) 
{ 
    using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString))) 
    { 
     var quotas = new XmlDictionaryReaderQuotas(); 
     return XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, quotas)); 
    } 
} 
+1

Dzięki .. :). niezła odpowiedź –