最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - deserialize json object in javascript - Stack Overflow

programmeradmin10浏览0评论

I am developing asp 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

I am developing asp 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

Share Improve this question edited Dec 18, 2019 at 8:42 ADyson 62.2k16 gold badges79 silver badges92 bronze badges asked Feb 5, 2018 at 10:12 Ravi ReddyRavi Reddy 1232 silver badges11 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论