How to convert JSON to XML in ASP.Webopius Web design Project Management SEO Graphic Design E Commerce.Just a short post today.I spent a while on Google trying to figure this out and thought it might help others if I posted a solution.Many web services now use JSON over XML as their method for data communication.JSON is lightweight compared to XML but is still open and readable.See the link to the right of this page for more information.JSONHowever, once youve received a JSON format response from a web service, what if you want to use this within an Xml.Document or XSLT package of some kind You need to convert it from JSON into XML.The approach described below does require you to create a data structure Class in C that maps to the JSON data and the principle is JSON data Deserialize to a C object Serialize to XML format.Well use a Word. Press post an an example.Imagine a Word. Press Web Service plugin that has a Get.Posts function. This function returns one or more Word.Press posts in JSON format, looking like this ID 1.This is the content of the post,posttitle An example Post,postcategory 0,postexcerpt ,poststatus publish,commentstatus open,pingstatus open,postpassword ,postname product test,toping ,pinged ,postmodified 2.In the example above, one Word.Press blog post is returned with an ID of 1.An example Post. Step 1.In C, the first step is to create a class to contain the JSON object.So, we create a Post class.Post. public int ID.For a successful deserialization of JSON into the C object, its important that the field names match the JSON data.Step 2. Next, we need to deserialize convert the JSON data into the C object.In this example, json.Response is a string containing the JSON data and were going to create a List of Post objects that contain all the posts returned by the Web Service.System. Web. Script.Serialization. Java.Php Serialize Object To Json' title='Php Serialize Object To Json' />Script.Serializer ser new Java.Script. Serializer.Listlt Post postdata ser.Deserializelt Listlt Post json.Response. Step 3.OK, now we have a variable postdata containing all the posts in a List object.This specification defines a set of algorithms for programmatic transformations of JSONLD documents.Restructuring data according to the defined transformations.Theres a new JSON library on the block that promises high performance with tricks like dynamic class shadowing and quick data binding.Each item is a Post object.Now, we can serialize this data into an XML object.In this example, well write the XML to a String but you could of course convert into an Xml.In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored for.Document. using System.IO. using System.Xml. using System.Xml. Serialization.String. Writer sw new String.Writer. Xml. Writer xmlw Xml.Writer. Createsw.Xml. Serializer serializer new Xml.SerializertypeofList.Xml. Serializer. Namespaces ns new Xml.Serializer. Namespaces.Add, Omit namespace.Serializexmlw, postdata, ns.XML sw. To. String.The Xml. Serializer. Beli Software Windows Xp Original . Namespaces lines above arent required, but they avoid the XML containing any generated namespace values.Thats it, your XML string the.XML will now look something like this lt Array.Of. Post lt Post lt ID 1.ID lt postauthor 1lt postauthor lt postdate 2.This is the content of the postlt postcontent lt posttitle An example Postlt posttitle lt postcategory 0lt postcategory lt postexcerpt lt poststatus publishlt poststatus lt commentstatus openlt commentstatus lt pingstatus openlt pingstatus lt postpassword lt postname product testlt postname lt toping lt pinged lt postmodified 2.Post lt Array. Of.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |