CSharpNewBee
CSharpNewBee

Reputation: 1981

JSON.NET XML to JSon

whilst trying to work on something else, i stumbled across JSON.NET, and have a quick question regarding the results.

I have a XML Field in sql, which i return in a data reader, I then run this through the following:

      XmlDocument doc = new XmlDocument();
      doc.LoadXml(rdr.GetString(0));
      en.Add(JsonConvert.SerializeXmlNode(doc));

en is a List as there could be many rows returns. the JSON that is created is as follows with real data modified but the structure intact:

  "{\"Entity\":{\"@xmlns:xsd\":\"http://www.w3.org/2001/XMLSchema\",\"@xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\",\"AKA\":{\"string\":[\"Name 1\",\"Name 2\"]},\"Countries\":{\"string\":[\"UK\",\"US\"]},\"IdentNumbers\":{\"string\":[\"Date Set 2\",\"Data Set 1\",\"Data Set 3\",\"Data Set 4\"]},\"PercentageMatch\":\"94\"}}"

So if there were 3 entries then msg.d would contain three values as can be seen from FireBug output below

firebug output

How do i loop through this information on the client side, and present it in a table?

EDIT

So for the table layout. Any single item needs to have a heading and its associated value, for any items that have one or more value, then i need the table to have a single heading with each item on a new line. Something similiar to this:

Heading 1 Single Item Value Heading 2 First Item Value \n Second Item Value Heading 2 Single Item Value

EDIT Ok, kind of getting to where I want it. i've produced this:

  success: function (msg) {
  var resultHtml = "";
  $.each(msg.d, function (i, entity) {
  //now entity will contain one row of data - you could access the following objects :
  //entity.AKA is an array with which you could loop with
  resultHtml += '<label><b>Countries:</b></label>';
  resultHtml += '<text>' + entity.Countries + '</text>';
  resultHtml += '<label><b>Ident:</b></label>';
  resultHtml += '<text>' + entity.IdentNumbers + '</text>';
  //etc
  });

Which produces the output of heading in bold with the value underneath. What I know need to work out, is how to only show one instance at a time, and have pages to move through :-) Any Idea?

Upvotes: 0

Views: 176

Answers (1)

krishwader
krishwader

Reputation: 11371

using $.each, maybe? Here's the syntax :

$.each(msg.d, function(i, entity) {
   //now entity will contain one row of data - you could access the following objects :
   //entity.AKA is an array with which you could loop with
   //entity.Countries
   //entity.IdentNumbers
   //etc
});

Then you could construct that table in your each loop. If you give me more info on how you'd want to set up your table (the format), we could help you on that.

Here's a fiddle for you. Resize the output window and check the table : http://jsfiddle.net/hungerpain/9KBDg/

Upvotes: 1

Related Questions