10

I am developing asp.net mvc application, Java script will call controller for every 3 seconds and it should return list of json objects.I need to show the list of objects in table.Here, I am getting [object Object].should we deserialize that objects,if yes,then how to deserialize them.

Below is my java script code


<script>
    var fun = set_Interval(my_Timer, 3000);
    function my_Timer() {       
        $.ajax({           
            url: '@Url.Action("FirstAjax", "Home")',
            //data: '{param : "value"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: successFunc,
            error: errorFunc
        });
        function successFunc(response) { 
            alert(response);    
        }
        function errorFunc() {            
            alert('error');
        }
    }
</script>

below is controller

public JsonResult FirstAjax()
{            
    var listt = AlgoLegsClass.DataGridAlgos;
    JavaScriptSerializer js = new JavaScriptSerializer();
    string ss =  js.Serialize(listt);
    return Json(ss, JsonRequestBehavior.AllowGet);
}

Output- After every 3 seconds the timer call the controller and it returns list of objects.In alert box it is displaying like "Parameter name":"Value".How can i get these values as i need to append this list to table

ADyson
  • 57,178
  • 14
  • 51
  • 63
Ravi Reddy
  • 123
  • 2
  • 11

1 Answers1

4

Since you specified dataType: "json" in your ajax options, jQuery will already have done the deserialising of the JSON string back into a JavaScript object for you automatically.

What you're seeing is just what alert() does with JavaScript objects/arrays by default when trying to make them visual as text.

Try

alert(JSON.stringify(response));

instead. This will print a version of your object which is serialised back to a JSON string and thus make it more human-readable.

Or you can just look in the Response section of the ajax call's entry in your Network tab (in the browser's Developer Tools).

Also if you're using a JsonResult (i.e. return Json...) you do not need to serialise the object beforehand - MVC will do it for you. So chances are you're getting double-serialised nonsense.

Simply

return Json(listt, JsonRequestBehavior.AllowGet); 

should work no problem.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • I have changed my code as you specified above, I am getting output as [{"parameter _1":"value","parameter_2":"value","parameter_3":"value"}]. – Ravi Reddy Feb 05 '18 at 10:54
  • Ok. Well it's valid JSON, so that's good. What were you expecting to see? I don't know what's in your `listt` variable. – ADyson Feb 05 '18 at 10:56
  • my list contains employee list, i need to get all employee details one by one with their fields like name,salary etc. – Ravi Reddy Feb 05 '18 at 10:58
  • Ok but a description of your object is no good to anyone, a list of employees could have 100 different structures. Show the definition of whatever is the datatype of `DataGridAlgos` , and / or the actual values contained in this object at the time you run your code. – ADyson Feb 05 '18 at 11:02
  • class Employee { Name {set;get;} Id {set;get;} salary {get;set;} . DataGridAlgos contains List of employees.Now we need to show all employee details one by one in alert box. – Ravi Reddy Feb 05 '18 at 11:06
  • So `DataGridAlgos` is defined like `public List DataGridAlgos { get; set; }` in the `AlgoLegsClass` object? Because if it is, I see no way your JSON would end up serialised like that. What's the real definition of it? What do you mean by a "list" exactly? – ADyson Feb 05 '18 at 11:07
  • public static List DataGridAlgos = new List(); Employee emp1 = new Employee(); emp1.Id = 1; emp1.Name = "ABC";emp1.salary = 2000; DataGridAlgos.Add(emp1); This way I am adding employee details in list – Ravi Reddy Feb 05 '18 at 11:13
  • See https://dotnetfiddle.net/uSQYDH - I don't see how it's possible for the code you've described to produce the result you mention. You've obviously described something wrongly or omitted some important detail. – ADyson Feb 05 '18 at 11:32
  • same way I did and I got the same output.But I need to get single parameter like we do in c#. foreach(var item in listt) { string name = item.name; } – Ravi Reddy Feb 05 '18 at 11:48
  • Please tell me how to get each parameter seperately – Ravi Reddy Feb 05 '18 at 11:48
  • Don't you know how to write a `for` loop in Javascript? What you've got is a JavaScript array containing some objects. Google how to loop through an array of objects and also how to display a specific property of an object. e.g. https://stackoverflow.com/a/40416380/5947043 is a simple example. There are hundreds and hundreds of others, on this site alone never mind the rest of the web. – ADyson Feb 05 '18 at 11:50
  • Here length property is not coming for array, so I am unable to iterate through loop – Ravi Reddy Feb 05 '18 at 11:59
  • See the JSFiddle again: https://dotnetfiddle.net/uSQYDH . Can't see your code so I don't know what you did wrong, but perhaps you didn't understand how to write the `for` loop. I included an example, it's not very complicated. And here is an alternative using `forEach`: https://dotnetfiddle.net/yDcpnr – ADyson Feb 05 '18 at 12:14