Created
July 23, 2013 14:45
-
-
Save hectron/6062902 to your computer and use it in GitHub Desktop.
These are examples of JSON responses that may be converted into XML using Json.NET. The short json object is able to be converted. However, the last JSON response can not be converted. An XMLException is raised stating: The ' ' character, hexadecimal value 0x20, cannot be included in a name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Xml; | |
using System.Xml.Linq; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace APISerializer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string xmlAPICall = "THIS WAS EDITED TO REMOVE ANY REAL DATA"; | |
string jsonAPICall = "THIS WAS EDITED TO REMOVE ANY REAL DATA"; | |
string xmlResult = "";// ConvertResponseFromUrl(xmlAPICall, "json"); | |
string jsonResult = ConvertResponseFromUrl(jsonAPICall, "xml"); | |
if (xmlResult.Length > 0 || jsonResult.Length > 0) | |
Console.WriteLine("\nComplete! Press any key to quit."); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Given a url and an expected response format, this returns a string with the content. | |
/// </summary> | |
/// <param name="url"></param> | |
/// <param name="desiredOutputType"></param> | |
static string ConvertResponseFromUrl(string url, string desiredOutputType) | |
{ | |
string output = ""; | |
if (url == null || desiredOutputType == null) | |
return output; | |
HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest; | |
string horizontalRule = "="; | |
for (int i = 0; i < 3; i++) | |
{ | |
horizontalRule += horizontalRule; | |
} | |
Console.WriteLine("{0}\nAttempting to perform a {1} REST call to the following url:\n{2}", | |
horizontalRule, desiredOutputType, url); | |
using (HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse) | |
{ | |
if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.OK) | |
throw new Exception(String.Format( | |
"Server error (HTTP {0}: {1}.", | |
httpWebResponse.StatusCode, | |
httpWebResponse.StatusDescription)); | |
Stream webStream = httpWebResponse.GetResponseStream(); | |
StreamReader responseReader = new StreamReader(webStream); | |
string responseString = responseReader.ReadToEnd(); | |
Console.WriteLine("\n{0}\n{1}:\n{2}\n{3}", horizontalRule, "Response", horizontalRule, responseString); | |
switch (desiredOutputType.ToLower()) | |
{ | |
case "xml": | |
output = JsonConvert.DeserializeXNode(responseString).ToString(); | |
//JObject jsonResponse = JObject.Parse(responseString); | |
//var item = (string) jsonResponse["data"][0]["headline"]; | |
//output = ""; | |
break; | |
case "json": | |
var document = new XmlDocument(); | |
document.LoadXml(responseString); | |
output = JsonConvert.SerializeXmlNode(document); | |
break; | |
} | |
Console.WriteLine("\n{0}\n{1}:\n{2}\n{3}", horizontalRule, "Converted Response", horizontalRule, output); | |
return output; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"num_reviews":"2", | |
"page_id":"17816", | |
"merchant_id":7165 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"headline":"ant bully", | |
"created_date":"2010/06/12", | |
"merchant_group_id":10126, | |
"profile_id":0, | |
"provider_id":10000, | |
"locale":"en_US", | |
"helpful_score":1314, | |
"locale_id":1, | |
"variant":"", | |
"bottomline":"Yes", | |
"name":"Jessie", | |
"page_id":"17816", | |
"review_tags":[ | |
{ | |
"Pros":[ | |
"Easy to Learn", | |
"Engaging Story Line", | |
"Graphics", | |
"Good Audio", | |
"Multiplayer", | |
"Gameplay" | |
] | |
}, | |
{ | |
"Describe Yourself":[ | |
"Casual Gamer" | |
] | |
}, | |
{ | |
"Best Uses":[ | |
"Multiple Players" | |
] | |
}, | |
{ | |
"Primary use":[ | |
"Personal" | |
] | |
} | |
], | |
"rating":4, | |
"merchant_id":7165, | |
"reviewer_type":"Verified Reviewer", | |
"comments":"fun to play" | |
}, | |
{ | |
"headline":"Ok game, but great price!", | |
"created_date":"2010/02/28", | |
"merchant_group_id":10126, | |
"profile_id":0, | |
"provider_id":10000, | |
"locale":"en_US", | |
"helpful_score":1918, | |
"locale_id":1, | |
"variant":"", | |
"bottomline":"Yes", | |
"name":"Alleycatsandconmen", | |
"page_id":"17816", | |
"review_tags":[ | |
{ | |
"Pros":[ | |
"Easy to Learn", | |
"Engaging Story Line" | |
] | |
}, | |
{ | |
"Describe Yourself":[ | |
"Frequent Player" | |
] | |
}, | |
{ | |
"Primary use":[ | |
"Personal" | |
] | |
}, | |
{ | |
"Best Uses":[ | |
"Kids" | |
] | |
} | |
], | |
"rating":3, | |
"merchant_id":7165, | |
"reviewer_type":"Verified Reviewer", | |
"comments":"This is a cute game for the kids and at a great price. Just don't expect a whole lot." | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment