locked
How to return json from myhub class ? RRS feed

  • Question

  • User1197377994 posted

    Dear sir/madam

    i want to return data in list in json format from the hub.Is returning json data is allowed from hub in signal r.

    Tuesday, November 14, 2017 10:06 AM

Answers

  • User-1838255255 posted

    Hi 4itguy,

    Based on your requirement, I do a test with the following sample to push JSON array data to client and extract data from message in client side, which works for me, you can refer to it.

    Client side function:

    chat.client.addDataToPage = function (mydata) {
        // Add the message to the page.
        //alert(JSON.stringify(mydata.value));
    
        var md = JSON.parse(mydata.value);
    
        $.each(md, function (index, val) {
            alert("id: " + val.id + "; name: " + val.name + "; adres: " + val.adres);
        })
    };
    

    Hub method: 

    public void Send(string name, string message)
    {
        List<mydata> d = new List<mydata>();
        d.Add(new mydata() { id = 1, name = "sada", adres = "asdad" });
        d.Add(new mydata() { id = 2, name = "sa", adres = "ad" });
    
        Clients.All.addDataToPage(new Container { value = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(d) });
    
    }
    
    public class Container
    {
        public string value { get; set; }
    }
    
    public class mydata
    {
        public int id { get; set; }
        public string name { get; set; }
        public string adres { get; set; }
    }
    

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 8:00 AM

All replies

  • User-1838255255 posted

    Hi 4itguy,

    According to your description, i know the json data is allowed return form hub, here are some sample about how to implement it, please check:

    Get a Json array from a SignalR hub function as a string in javascript using signalR: 

    https://stackoverflow.com/questions/31697187/get-a-json-array-from-a-signalr-hub-function-as-a-string-in-javascript-using-sig 

    How to pass JSON back and forth from signalr hub & js code: 

    https://forums.asp.net/t/2027743.aspx?How+to+pass+JSON+back+and+forth+from+signalr+hub+js+code 

    Best Regards,

    Eric Du

    Wednesday, November 15, 2017 3:31 AM
  • User1197377994 posted

    Hi Eric Du

    thanks for the help but i am unable to fetch the data out of the object

    thats myhub code

     public void onconn() //the function will get called on hub start
            {
                List<data> d = new List<data>();
               d = co.Query<data>("select * from Udata").AsList();
               Clients.All.send(new Container {value= new JavaScriptSerializer().Serialize(d) });
            }
            public class Container
            {     
                public string value { get; set; }
            }

    im getting the value like :- value:"[{"id":1,"name":"sada","adres":"asdad"},{"id":2,"name":"sa","adres":"ad"}]"

    but not able to fetch data out from this.I used foreach in jquery but didn't worked.

    Can you help a little more in this.

    Wednesday, November 15, 2017 4:04 AM
  • User-1838255255 posted

    Hi 4itguy,

    Based on your requirement, I do a test with the following sample to push JSON array data to client and extract data from message in client side, which works for me, you can refer to it.

    Client side function:

    chat.client.addDataToPage = function (mydata) {
        // Add the message to the page.
        //alert(JSON.stringify(mydata.value));
    
        var md = JSON.parse(mydata.value);
    
        $.each(md, function (index, val) {
            alert("id: " + val.id + "; name: " + val.name + "; adres: " + val.adres);
        })
    };
    

    Hub method: 

    public void Send(string name, string message)
    {
        List<mydata> d = new List<mydata>();
        d.Add(new mydata() { id = 1, name = "sada", adres = "asdad" });
        d.Add(new mydata() { id = 2, name = "sa", adres = "ad" });
    
        Clients.All.addDataToPage(new Container { value = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(d) });
    
    }
    
    public class Container
    {
        public string value { get; set; }
    }
    
    public class mydata
    {
        public int id { get; set; }
        public string name { get; set; }
        public string adres { get; set; }
    }
    

    Result:

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 8:00 AM
  • User1197377994 posted

    Hi Eric Du

    Thanks a lot for helping.

    Wednesday, November 15, 2017 9:03 AM