本文介绍了如何将Json对象转换为Jquery数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过ajax调用从Web方法获取Year和'NoOfOrders'.我想将Json对象转换为数组,以便可以使用jqbargraph进行绘制.

I am getting Year and 'NoOfOrders' from a Web Method through ajax call.I want to convert Json object to array so that I can plot using jqbargraph.

$(document).ready(function () {
    var myArray = [];
    $("[id$=btnSubmit]").click(function () {
        $.ajax({
            type: "POST",
            url: "Charts.aspx/GetOrderCount",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var OrdersInfo = data.d;
                $.each(OrdersInfo, function (index, order) {
                    myArray.push(order.OrderYear, order.OrderCount);
                });
            }
        });
        $('#divChart').jqbargraph({
            data: myArray,
            position: 'bottom',
            animate: false
        });
    });
});

推荐答案

尝试推送一个数组,而不是将单独的值放入myArray:

Try to push an Array, instead of separate values into myArray:

$.each(OrdersInfo, function (index, order) {
      myArray.push([order.OrderYear, order.OrderCount]);
});

这篇关于如何将Json对象转换为Jquery数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:33