Most efficient way to fix an invalid JSON
You need to run this through JavaScript. Fire up a JavaScript parser in .net. Give the string as input to JavaScript and use JavaScript's native JSON.stringify
to convert:
obj = {
"user":'180111',
"title":'I\'m sure "E pluribus unum" means \'Out of Many, One.\' \n\nhttp://en.wikipedia.org/wiki/E_pluribus_unum.\n\n',
"date":'2007/01/10 19:48:38',
"id":"3322121",
"previd":"112211",
"body":"\'You\' can \"read\" more here [url=http:\/\/en.wikipedia.org\/?search=E_pluribus_unum]E pluribus unum[\/url]'s. Cheers \\*/ :\/",
"from":"112221",
"username":"mikethunder",
"creationdate":"2007\/01\/10 14:04:49"
}
console.log(JSON.stringify(obj));
document.write(JSON.stringify(obj));
Please remember that the string (or rather object) you've got isn't valid JSON and can't be parsed with a JSON library. It needs to be converted to valid JSON first. However it's valid JavaScript.
To complete this answer: You can use JavaScriptSerializer
in .Net. For this solution you'll need the following assemblies:
- System.Net
System.Web.Script.Serialization
var webClient = new WebClient(); string readHtml = webClient.DownloadString("uri to your source (extraterrestrial)"); var a = new JavaScriptSerializer(); Dictionary<string, object> results = a.Deserialize<Dictionary<string, object>>(readHtml);
How about this:
string AlienJSON = "your alien JSON";
JavaScriptSerializer js = new JavaScriptSerializer();
string ProperJSON = js.Serialize(js.DeserializeObject(AlienJSON));
Or just consume the object after deserialize instead of converting it back to string and passing it to a JSON parser for extra headache
As Mouser also mentioned you need to use System.Web.Script.Serialization which is available by including system.web.extensions.dll in your project and to do that you need to change Target framework in project properties to .NET Framework 4
.
EDIT
Trick to consume deserialized object is using dynamic
JavaScriptSerializer js = new JavaScriptSerializer();
dynamic obj = js.DeserializeObject(AlienJSON);
for JSON in your question simply use
string body = obj["body"];
or if your JSON is an array
if (obj is Array) {
foreach(dynamic o in obj){
string body = obj[0]["body"];
// ... do something with it
}
}