nishanth yeddula
nishanth yeddula

Reputation: 77

Displaying the JSON string

This is what I have done

 $.ajax({
            type: "POST", url: "Data.aspx/CheckInsertRecord",
            data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," +
                   "EndDate:'" + enddate+"' }",
            contentType: "application/json; charset=utf-8", dataType: "json",
            success: function (msg) {
            var data = $.parseJSON(msg.d);

           alert("A record of this event already exists in the database.
                  \n" +  msg.d+".");

            }});

SO what happens in code behind is:

public static string CheckInsertRecord(String EventType, String BeginDate, String EndDate)
{
    NCDCPoint ncdc = new NCDCPoint();
    CEOSurveyDataContext CDC = new CEOSurveyDataContext();
    int et = Convert.ToInt32(EventType);
    CultureInfo provider = CultureInfo.InvariantCulture;
    DateTime b = Convert.ToDateTime(BeginDate);
    DateTime e = Convert.ToDateTime(EndDate);
    DetailsView a = new DetailsView();



  var query = (from n in CDC.NCDCPoints
                where n.EVENT_TYPE_ID == et && n.BeginDate == b && n.EndDate == e
                select new { 
                   n.EVENT_TYPE_ID,
              BeginDate =  n.BeginDate.ToString("yyyy-MM-dd",provider),
               EndDate = n.EndDate.ToString(),
                n.BeginLAT,
              BeginLONG =  n.BeginLONG,
                n.EndLAT,
                n.EndLONG});

   if (query.Any())
   {
       return new JavaScriptSerializer().Serialize(query.ToList());
   }
   else
   {
       return "No duplicate";
   }

      }

The code may not be of much use. However, the JOSN string would be displayed pretty ordinarily. I tried to use details view but was not successful. SO, can u guys please tell me the way so that I can print JOSN string a good format. This is how my JSON string is displayed right now:

{"EVENT_TYPE_ID":1,"BeginDate":"2011-06-03","EndDate":"2011-06-11",
 "BeginLAT":null,"BeginLONG":null,"EndLAT":null,"EndLONG":null}

Upvotes: 0

Views: 1167

Answers (4)

Pit Digger
Pit Digger

Reputation: 9800

If you want to display after processing it . Take a look at http://james.newtonking.com/pages/json-net.aspx

You can de-serialize it as object and print it nicely.

Upvotes: 0

sandyiit
sandyiit

Reputation: 1705

Although I am not clear of the problem but it looks like you are trying to call the C# code from jquery and you are successful to do so.

To get the JSON object in your code, just try:

<html>

    function test (data) 
    { 
        var a = $.parseJSON(data);
        alert(a.EVENT_TYPE_ID);
    }
    function load()
    {
        test('{"EVENT_TYPE_ID":1,"BeginDate":"2011-06-03","EndDate":"2011-06-11","BeginLAT":null,"BeginLONG":null,"EndLAT":null,"EndLONG":null}');
    }
</script>
</head>
<body onload="load()">
</body>
</html>

It works for me.

Upvotes: 0

dubRun
dubRun

Reputation: 245

If this is simply for your debugging needs, then I would suggest using Firebug for Firefox. It will show you all of the AJAX requests and the server response coming back. If the response is properly formatted JSON it will give a nice collapsible view of the JSON.

Upvotes: 2

glosrob
glosrob

Reputation: 6715

If I understand your requirements you want to display the JSON returned.

Could you not make use of the data variable you are setting with $.parseJSON?

That would provide you with an object, containing the JSON values.

e.g.

 var data = $.parseJSON(msg.d);

would give you something like

 data.EVENT_TYPE_ID
 data.BeginDate
 //etc

so you could use this to build up a display string in whatever format suited you best.

Upvotes: 0

Related Questions