Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I understand you question very good. I like and have been using XML and XML schema since many years. Since a half of year I use JavaScript and jQuery together with RESTfull WFC services, ASP.NET MVC technologies with JSON encoded data for transfer. So I had to ask me the same question some time ago. Here is my answer with a short explanation.</p> <p>The XML structure which you define can represent <strong>one input parameter</strong> of our Web Service. If you prefer divide XML structure to different input parameters (like Locale, Layout and Query) all what I write here can be easy modified to this case of parameter coding.</p> <p>The most native way in my opinion is the way which we implement <strong>using out-of-the-box tools</strong>. So let us we have our Web Service with a method like <code>MyMethod</code> and an input parameter <code>param</code>1 with the type <code>MyData</code> (something like <code>public int MyMethod (MyData param1)</code>). Class <code>MyData</code> represent our all data from the XML structure. Then the corresponding HTTP GET request should look like </p> <p><a href="http://server/Service/MyMethod?param1=JsonEncodedData" rel="nofollow noreferrer">http://server/Service/MyMethod?param1=JsonEncodedData</a></p> <p>where <strong>JsonEncodedData</strong>:</p> <p>%7B%22locale%22%3A%22en%22%2C%22layout%22%3A%5B%7B%22id%22%3A%22header%22%2C%22value%22%3A%22hide%22%7D%2C%7B%22id%22%3A%22footer%22%2C%22value%22%3A%22hide%22%7D%2C%7B%22id%22%3A%22navigation%22%2C%22value%</p> <p>or the same without HTML encoding:</p> <p>{"locale":"en","layout":[{"id":"header","value":"hide"},{"id":"footer","value":"hide"},{"id":"navigation","value":"minimize"}],"query":{"what":"water","when":{"Start":{"Year":2010,"Month":1,"Day":1}}}</p> <p>This data is a JSON encoded JavaScript object </p> <pre><code>var myInput = { locale: "en", layout:[ {id: "header", value: "hide"}, {id: "footer", value: "hide"}, {id: "navigation", value: "minimize"} ], query:{ what: "water", when: { Start:{Year:2010,Month:1,Day:1} } } }; </code></pre> <p><strong>REMARK</strong>: Because <code>Date</code> class of JavaScript can be serialized in a little different ways I changed a little the representation of start date here. In the "Microsoft world" we can use <code>Sys.Serialization.JavaScriptSerializer.serialize</code> to serialize <code>DateTime</code> .NET type which will encode 2010-01-01 like "/Date(1262300400000)/".</p> <p>We can write in C# an easy Web Method of Web Service Service1.asmx like</p> <pre><code>[WebMethod] [ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public int MyMethod (MyData param1) { return param1.query.when.Start.Year; } </code></pre> <p>where</p> <pre><code>public enum BlockType { hide, minimize } public class Block { public string id { get; set; } //public BlockType value { get; set; } public string value { get; set; } } public class MyDate { public int Year { get; set; } public int Month { get; set; } public int Day { get; set; } } public class When { public MyDate Start { get; set; } } public class Query { public string what { get; set; } public When when { get; set; } } public class MyData { public string locale; public List&lt;Block&gt; layout; public Query query; } </code></pre> <p>And the corresponding client code will looks like following</p> <pre><code>var myInput = { locale: "en", layout:[ {id: "header", value: "hide"}, {id: "footer", value: "hide"}, {id: "navigation", value: "minimize"} ], query:{ what: "water", when: { Start:{Year:2010,Month:1,Day:1} } } }; $.ajax({ type: "GET", url: "/Service1.asmx/Method2", data: {param1: JSON.stringify(myInput)}, contentType: "application/json; charset=utf-8", success: function(data, textStatus, XMLHttpRequest) { alert(XMLHttpRequest.responseText); }, error: function(res, status) { if (status ==="error") { // errorMessage can be an object with 3 string properties: // ExceptionType, Message and StackTrace var errorMessage = $.parseJSON(res.responseText); alert(errorMessage.Message); } } }); </code></pre> <p>For JSON encoding I use in the example json2.js (see <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="nofollow noreferrer">https://github.com/douglascrockford/JSON-js/blob/master/json2.js</a> and <a href="http://www.json.org/js.html" rel="nofollow noreferrer">http://www.json.org/js.html</a>). One can use instead JSON jQuery (from <a href="http://code.google.com/p/jquery-json/" rel="nofollow noreferrer">http://code.google.com/p/jquery-json/</a>), then <code>JSON.stringify</code> should be replaces to <code>$.toJSON</code>.</p> <p>Initializing of data of the type <code>MyData</code> can look like</p> <pre><code>new MyData { locale = "en", layout = new List&lt;Block&gt; { new Block() { id="header", value=BlockType.hide.ToString()}, new Block() { id="footer", value=BlockType.hide.ToString()}, new Block() { id="navigation", value=BlockType.minimize.ToString()} }, query = new Query { what = "water", //when = new When() {Start = new DateTime(2010,1,1)} when = new When () { Start = new MyDate () { Year = 2010, Month = 1, Day = 1 } } } }; } </code></pre> <p>If you use WCF with webHttpBinding (RESTfull endpoints) instead of asmx based web services you can receive more flexible solution.</p> <p>If use use JSON with HTTP GET you should take in consideration existence of the JSON Hijacking problem (see <a href="http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx</a>, <a href="http://haacked.com/archive/2009/06/25/json-hijacking.aspx" rel="nofollow noreferrer">http://haacked.com/archive/2009/06/25/json-hijacking.aspx</a>). This problem can be solved (or dramatically reduced) in the different ways and it don't stop me from the usage of JSON in HTTP requests, which can be perfect cached.</p> <p>For more information about the same subject you can look through my answer to a close questions: <a href="https://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086">How do I build a JSON object to send to an AJAX WebService?</a> <a href="https://stackoverflow.com/questions/2670147/can-i-return-json-from-an-asmx-web-service-if-the-contenttype-is-not-json/2671583#2671583">Can I return JSON from an .asmx Web Service if the ContentType is not JSON?</a> <a href="https://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working/2656543#2656543">JQuery ajax call to httpget webmethod (c#) not working</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for the verbose post. The problem with JSON encoding is that it is very hard to handle it manually. Controls symbols are URL-encoded, the brackets must match and so on. This syntax is surely suitable for JS-programmers, but it's not friendly for "end"-users which are our target group in this case. We've finally come to the conclusion that there's no "standard" encoding, so we've developed our own syntax. I'll describe it when we're ready with the parser.
      singulars
    2. COSorry but It seems to me nobody (as a person) will be manually build and call an URL with more then 10 parameters. So you should write a program from both side (client and server) which encode and decode some structures represented the data. I try to suggest a native way for both JavaScript and .NET Web Service with a simplest de- and JSON encoding. Disadvantage of encoding from your question is that both client and server have to hold not a structures, but many separated variables. What is better?
      singulars
    3. COThe requirements are present and not imaginary. This is what our clients need. Actually, there's a lot of APIs like this - there's PayPal NVP, there's Google Charts, there are OGC things like WMS and so on. Th "end-user" is a someone like professional system administrator or application maintainer who needs a simple URL API to integrate our application in his/her environments. Yes, we can provide additional instruments like "here's a thingy you'll have to build your URLs with", but this must not be exclusive since it lowers the acceptance (which influences the sales numbers).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload