0

I am using the following code to convert JSON string to XML for processing in C#:

XmlDictionaryReader xdr = JsonReaderWriterFactory.CreateJsonReader(System.Text.Encoding.Unicode.GetBytes(jsonStr), new XmlDictionaryReaderQuotas()); // @@@ QUOTA MAX WILL FIX THE MISSING CLOSING TAGS? 
XElement root = XElement.Load(xdr); // Here I get the exception: The token '"' was expected but found '

I have used it with no problem until I tried to parse JSON with strings not enclosed by double quotes (which by the way, I think is perfectly fine with JSON format)

For example:

{string:"value"}

Can any body tell me how can I serialize JSON in C# so that this kind of simple format particulary wont break the code or generate exceptions?

EDIT: I think what I am reading is not a JSON object but a javascript object like the following: What's the difference between Javascript Object and JSON object

So question would be: How to deserialize javascript objects to XML (not JSON) in C#?

Community
  • 1
  • 1
Devela
  • 564
  • 1
  • 6
  • 25
  • You should double check JSON standard. Will notice that you can omit the double quote in the string part. (sorry I overwrote your question, its self explanatory anyway) – Devela Dec 01 '12 at 01:01
  • Can you pass this through a browser library by any chance? I expect you're getting back data as a webservice, and can't do anything that isn't C#, yeah? – jcolebrand Dec 01 '12 at 01:05
  • I need C# to deserialize it. However it seems to me is not JSON but a Javascript Object. – Devela Dec 01 '12 at 01:07
  • 1
    No, it's just a poorly formed JSON object. There is no such thing as a "javascript object" like this. I can appreciate "accept garbage and give good results" but what you probably need to do is just enforce valid JSON input, if you can. It will save everyone lots of headache. – jcolebrand Dec 01 '12 at 01:08
  • Well that's just a javascript object declaration now that you've pointed to that link. Where exactly are you getting this information from that you're trying to parse in the first place? You're running a WCF, obviously, so it must be coming from clients. Refuse to service malformed requests, as Microsoft, IBM and Apple would do. – jcolebrand Dec 01 '12 at 01:16
  • Is not malformed JSON, is just a javascript object. Check link in edit. @jcolebrand – Devela Dec 01 '12 at 01:17
  • I did, hence I've asked the question twice now. Where, please answer this question, is the data coming from? I really want to help you, but you're just going "no no no, you're wrong" and all I've done is ask you for details that you failed to provide. – jcolebrand Dec 01 '12 at 01:18
  • Is the way Google send results! – Devela Dec 01 '12 at 01:19
  • Where are you getting the google results from? From a search page request? – jcolebrand Dec 01 '12 at 01:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20421/discussion-between-devela-and-jcolebrand) – Devela Dec 01 '12 at 01:19
  • 1
    @Devela `dynamic obj=JsonConvert.DeserializeObject("{str:\"test\",i:5}"); Console.WriteLine(obj.str + " " + obj.i);` works with Json.Net – L.B Dec 01 '12 at 17:01
  • Yeah I tried JSON.NET however, now it would complain about escaping characters like \x ... so I fixed that, but still I think I am not getting the wanted result. – Devela Dec 02 '12 at 04:22

0 Answers0