如何将对象数组从C#发送到JavaScript?

时间:2019-01-31 21:53:29

标签: javascript c# jquery json

我试图通过SignalR中建立的连接发送包含一些对象的数组,该连接不是问题,一切正常。当数据到达视图时,它不再是我需要使用的数组。 这是课程:

public class Empresa
    {
        public string nombre { get; set; }
        public int vidID { get; set; }
        public string img64 { get; set; }
        public string color { get; set; }

    }

最后,对象将是这样的:

Sample

对象被发送到视图,这是输出:

Output

我已经尝试在其他线程上使用JsonConvert.SerializeObject,但是它似乎没有用。我尝试转换与此jQuery.parseJSON(data)(左)和此JSON.parse(data)(右)发送的数据;如下图所示,这两种情况都会引发错误。

Error, LEFT = jQuery.parseJSON  RIGHT = JSON.parse

我不确定是否是这样,因为发送的对象是这样制作的:

private readonly ConcurrentDictionary<int, Empresa> _ar1 = new ConcurrentDictionary<int, Empresa>();

var data = new List<Empresa>
{
   new Empresa{nombre ="Globex Corp",color="red",vidId=1, img="data:image/jpeg;base64,blabla" },
   new Empresa{nombre ="AM",color="blue",vidId=2, img="data:image/jpeg;base64,blabla" }
}
for(int i = 0; i<=6; i++)
{
    _ar1.TryAdd(data[i].vidID, data[i]);
}

这是其他函数的内部,但这是涉及数据发送的下一个函数。

public IEnumerable<Empresa> GetArreglo()
        {
           return _ar1;
        }

到目前为止,我不确定是否可能出问题或我是否需要提出其他解决方案。 如果需要更多信息,请张贴。甚至很明显,一个新人仍然在学习这一点。

编辑:

这是所有涉及的代码:

// This is the JS
<script>

        var ubi = '@ViewBag.ubicacion';
        console.log("Ubicación: " + ubi);
        var conex = $.connection.channel;
        var $marco = $('#marco');
        var $imagen = $('#imagen');
        var $empresa = $('#empresa');

        function empezar() {

            var min;
            var max;
            var pos;
            var arreglo = new Array;



            function init() {

                conex.server.createGroup(ubi);
                console.log("Entro al canal");
                arreglo = conex.server.getArreglo(ubi);
                //pos = arreglo.split('|');
                //a.split is not a function
                console.log(arreglo);
                //console.log(pos);
                setInterval(update, 6000);

            }

            function update() {


            }

            $.connection.hub.start().done(init);
        }

        window.onload = function() { empezar(); }
    </script>

    //It gets the conection to the HUB:

    [HubName("channel")]
    public class CanalHub : Hub
    {
        private readonly Canal _canal;

        public CanalHub() : this(Canal.Instance) { }

        public CanalHub(Canal canal)
        {
            _canal = canal;
        }



        public string[] GetArreglo(string ubi)
        {
            string[] array = _canal.GetArreglo(ubi);
            return array;
            //it is now a string[] because i wanted to
            //try creating the obj with .split('|')
        }


// And finally this is the last part involved:

 public class Canal
    {

        private static Random random = new Random();
        private volatile List<Canales> listaCan = new List<Canales>();
        private readonly static Lazy<Canal> _instance = new Lazy<Canal>(() => new Canal(GlobalHost.ConnectionManager.GetHubContext<CanalHub>().Clients));
        private readonly ConcurrentDictionary<int, Empresa> _datos = new ConcurrentDictionary<int, Empresa>();
        private readonly ConcurrentDictionary<int, Empresa> _ar1 = new ConcurrentDictionary<int, Empresa>();

        private Canal(IHubConnectionContext<dynamic> clients)
        {
            Clients = clients;

            //Create the sample objects for the class
            var datos = new List<Empresa>
            {
                new Empresa{nombre="Globex Corp", color="#A87F3D", vidID=1, img="balbal" },
                new Empresa{nombre="AM", color="#535E89", vidID=2, img="balba" },
                new Empresa{nombre="Frutijugos", color="#92191A", vidID=3, img="askldj" }
    };


            for (int i = 0; i <=6 ; i++)
            {
                _ar1.TryAdd(datos[i].vidID, datos[i]);
            }
            for (int i = 7; i <= 13; i++)
            {
                _ar2.TryAdd(datos[i].vidID, datos[i]);
            }
            for (int i = 14; i <= 20; i++)
            {
                _ar3.TryAdd(datos[i].vidID, datos[i]);
            }
            //sort them on 3 different arrays
        }

        private IHubConnectionContext<dynamic> Clients { get; set; }

        public static Canal Instance
        {
            get { return _instance.Value; }
        }


        public string[] GetArreglo(string ubi)
        {
            string[] array = new string[7];
            int i = 0;

            if (ubi == "Campanario")
            {
                foreach (var item in _ar1)
                {
                    array[i] += item.Value.nombre + "|";
                    array[i] += item.Value.color + "|";
                    array[i] += item.Value.img + "|";
                    array[i] += item.Value.vidID + "|";
                    i++;
                }

                return array;
            }
            //sort the array values and add them to the array

            else return null;          
        }

1 个答案:

答案 0 :(得分:1)

您的JavaScript承诺似乎设置不正确。视图中的对象是Promise对象,而不是返回的对象。您将需要正确设置承诺。 deferred promise