0

I am looking for a code example for JsonReaderWriterFactory.CreateJsonWriter to convert XML to JSON without having to install external dll's. But all examples like this utilize DataContractJsonSerializer and convert XML like this:

<temp>42</temp>

to produce JSON like:

"\"<temp>42<\\/temp>\""

instead of, what I am really looking for:

{"temp":42}

Apparently, both outputs are JSON, but how do I obtain the latter kind?

By the way, my input is an XElement, not XmlElement.

I already found code to convert JSON to XML using the opposite method JsonReaderWriterFactory.CreateJsonReader, which works kind of nice, and does NOT utilize DataContractJsonDeserializer or something like that. So I guess I need to find sample code for JsonReaderWriterFactory.CreateJsonWriter without DataContractJsonSerializer. Unfortunately, the MSDN pages like this on CreateJsonWriter do not show code samples.


Update:

I am giving up on this question. Apparently, I should need to switch to the NewtonSoft dll.

The purpose of my question was to quickly create a conversion to translate a sample xml data file, and it is always nice to have a new tool in my personal Lib. Now I will just use an online conversion tool for this one xml.

Thanks all of you for pointing out that I really need to get the Newtonsoft dll if I want to do further JSON stuff.


Comments:

  • similar to possible duplicate questions: very likely that others tried something like I did, because it sounds attractive, but there is no conclusive answer that it is just not possible

  • other seemingly duplicate questions almost always focus on the Newtonsoft dll, while my question is on the native dll's.


Duplicate:

Looks so, but I point out that the output is some wrong kind of correct JSON, and ask for a way of calling that method to get 'useful' JSON.

The commenters point out details about why this happens, namely the native dll has limitations.

Community
  • 1
  • 1
Roland
  • 4,619
  • 7
  • 49
  • 81
  • That is about using a downloaded dll from NewtonSoft, probably very useful, but I would like to try the native dll first. Also has an example using Serialization, what gives me the wrong kind of json as mentioned. – Roland Jul 12 '16 at 11:05
  • It is not only about JSON.NET. The accepted answer contains JSON.NET solution (since it is the most simple), but there are other answers. For example, this one uses .NET Framework native classes: http://stackoverflow.com/a/32412316/3218692 – Yeldar Kurmangaliyev Jul 12 '16 at 11:06
  • @YeldarKurmangaliyev I am trying this out. Just need to find the reference for System.Web.Script . . . – Roland Jul 12 '16 at 11:15
  • 1
    I tried using `JsonReaderWriterFactory.CreateJsonWriter()` to convert XML to JSON a while back and it didn't really work, because it had all sorts of restrictions on the XML, including having a specific root element name, no namespaces, and special attributes to indicate whether elements map to JSON objects or arrays. – dbc Jul 12 '16 at 11:18
  • Most useful doc page is probably [Mapping Between JSON and XML](https://msdn.microsoft.com/en-us/library/bb924435.aspx). – dbc Jul 12 '16 at 11:22
  • @dbc I guess that this makes `JsonReaderWriterFactory.CreateJsonWriter` useless for me. Thanks for pointing out details. – Roland Jul 12 '16 at 11:30
  • @dbc If your comment were an "Answer", I'd accepted it, because it points out 'why' CreateJsonWriter is not useful. – Roland Jul 13 '16 at 07:53

2 Answers2

1

I think you should use using Newtonsoft.Json; anyway. I guess it's one of the most popular .net library for working with json.

And you can install it by nuget

See this SO answer

// 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 = JsonConvert.DeserializeXmlNode(json);

Some guide you can find here. Also this article might be useful

For XDocument please see this SO answer

Community
  • 1
  • 1
isxaker
  • 8,446
  • 12
  • 60
  • 87
0

You can convert Xml document to JSON using json.net

http://www.newtonsoft.com/json/help/html/convertingjsonandxml.htm

sawbeanraz
  • 187
  • 10
  • Do you mean it is not possible with JsonReaderWriterFactory.CreateJsonWriter from the native dll? – Roland Jul 12 '16 at 11:06
  • Nope...JSON.Net will makes our life easy... JsonReaderWriterFactory require extra work.. that's it – sawbeanraz Jul 12 '16 at 11:10
  • Preferences, OK, but if I just got JsonReaderWriterFactory to work that would be nice. However, if the microsoft does not work or is lacking essential documentation, I will definitely go for JSON.Net – Roland Jul 12 '16 at 11:18
  • If JSON.Net is not an option for you then check this answer http://stackoverflow.com/questions/26157441/is-it-possible-to-use-jsonreaderwriterfactory-to-convert-xml-to-json-without-usi this leads you to this artile http://www.phdcc.com/xml2json.htm They don't use JSONReaderWriterFactory but does not use any external library – sawbeanraz Jul 12 '16 at 11:21
  • Thanks for the link, but I tried that approach and got the wrong kind of json output, as mentioned – Roland Jul 12 '16 at 11:30