Json to XML

Mar 18, 2008 at 2:54 PM
I wanted to translate JSON to XML.

I have the following JSON response from Panoramio API:

string strJSON = @"{
""count"": 773840,""photos"": [
{
""photo_id"": 532693,
""photo_title"": ""Wheatfield in afternoon light"",
""photo_url"": ""http://www.panoramio.com/photo/532693"",
""photofileurl"": ""http://static2.bareka.com/photos/medium/532693.jpg"",
""longitude"": 11.280727,
""latitude"": 59.643198,
""width"": 500,
""height"": 333,
""upload_date"": ""22 January 2007"",
""owner_id"": 39160,
""owner_name"": ""Snemann"",
""owner_url"": ""http://www.panoramio.com/user/39160"",
},
{
""photo_id"": 505229,
""photo_title"": ""Etangs près de Dijon"",
""photo_url"": ""http://www.panoramio.com/photo/505229"",
""photofileurl"": ""http://static2.bareka.com/photos/medium/505229.jpg"",
""longitude"": 5.168552,
""latitude"": 47.312642,
""width"": 350,
""height"": 500,
""upload_date"": ""20 January 2007"",
""owner_id"": 78506,
""owner_name"": ""Philippe Stoop"",
""owner_url"": ""http://www.panoramio.com/user/78506""
}, ...
]
}";

I want to build an XML from this JSON.

I can retrieve the object succesfully with:

object objJSON = JavaScriptConvert.DeserializeObject(strJSON);

But I would need to convert this to XML. How could this be done?

I've tried with

XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(strJSON);

But I get the following exception:

'JavaScriptConvert.DeerializeXmlNode(tmp)' threw an exception of type 'System.InvalidOperationException' base {System.SystemException}: This document already has a 'DocumentElement' node.

Thanks for your attention.
Coordinator
Mar 29, 2008 at 10:49 AM
The root object has multiple properties. The root object must have a single property in order to create a valid XML document.
Apr 21, 2008 at 10:23 AM
So should I build a root item, e.g., ID, and then attach the defined JSON as a child?

Any idea of how to program this?

Thanks a lot for your attention.
Coordinator
Apr 26, 2008 at 1:41 AM
If you are using Json.NET 2.0 you could read the JSON into a JObject, create a new JObject and add the populated JObject as a child property with the name "root". You could then pass that to the JsonSerializer along with the XmlNodeConverter to create an XmlDocument.
May 15, 2008 at 12:00 AM
Edited May 15, 2008 at 12:25 AM

Hi there, I am newbie to JSON. I have similar kind of JSON, I need to convert it to XML using C#, I have downloaded the JsonExSerializer but not sure how do I use it in this situation, Could you please share the code with me?
Here is my JSON
"fetalDevelopment":
{"months":

 

{"month":[

{"name":"month1", "linkContent":

[{"documentType":"MedicalTest","description":"Pregnancy test/hCG","rank":"1","documentHref":"hw42062","sectionHref":"hw42065"},

{"documentType":"MedicalTest","description":"Home pregnancy tests","rank":"1","documentHref":"hw227606","sectionHref":"hw227609"},

{"documentType":"Special","description":"Pregnancy","rank":"1","documentHref":"hw197814","sectionHref":"hw197816"},

{"documentType":"Frame","description":"Preparing for a healthy pregnancy","rank":"3","documentHref":"hw195050","sectionHref":"hw195050-sec"},

{"documentType":"Definition","description":"How to count the weeks","rank":"4","documentHref":"tn10321","sectionHref":"tn10321-sec"}]},

{"name":"month2","linkContent":

[{"documentType":"MedicalTest","description":"Pregnancy test/hCG","rank":"1","documentHref":"hw42062","sectionHref":"hw42065"},

{"documentType":"MedicalTest","description":"Home pregnancy tests","rank":"1","documentHref":"hw227606","sectionHref":"hw227609"},

{"documentType":"Special","description":"Pregnancy","rank":"1","documentHref":"hw197814","sectionHref":"hw197816"},

{"documentType":"Frame","description":"Preparing for a healthy pregnancy","rank":"3","documentHref":"hw195050","sectionHref":"hw195050-sec"},

{"documentType":"Definition","description":"How to count the weeks","rank":"4","documentHref":"tn10321","sectionHref":"tn10321-sec"}]}

]}

}

 

 

 

 

Thanks,
leo


Coordinator
May 17, 2008 at 5:14 AM
I don't know anything about JsonExSerializer. You'll have to talk to the person who made it.
Apr 8, 2009 at 4:36 PM

James,

A few things:
1) I am using Json.NET on the .net framework 2.0 and can not find a reference to the JObject. Is this in the 2.0 version? If so What do I have to reference to achieve this? Are you referring to a JavaScriptObject? How would I go about this if this is the case?

2) In my program I am reading in
http://api.yelp.com/business_review_search?term=yelp&tl_lat=37.9&tl_long=-122.5&br_lat=37.788022&br_long=-122.399797&num_biz_requested=3&ywsid=WuWVEa9BX4ITePrEnJ1euA

By Calling this is a browser it returns valid JSONbut I am still getting the error. My Code is as follows.
 
        XmlDocument xmlDoc = new XmlDocument();
       string sUrl = HttpGet("http://api.yelp.com/business_review_search?term=yelp&tl_lat=37.9&tl_long=-122.5&br_lat=37.788022&br_long=-122.399797&num_biz_requested=3&ywsid=WuWVEa9BX4ITePrEnJ1euA"); // Reads in Response Stream
        xmlDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(sUrl);
        Response.Write(xmlDoc.InnerText);

Thanks in advance.

Michael

Jul 6, 2010 at 8:26 PM
Michael, I know this was a while ago, but I am trying to do pretty much the same thing in VB.Net and am getting the same error message. Were you able to resolve this? If so, how? Thanks, Jon