Link to home
Start Free TrialLog in
Avatar of websss
websssFlag for Kenya

asked on

access C# object from Javascript - need help converting to the correct object in JS

I have an object in c# which is

_temperatureData = new float[320, 240];

Open in new window


it looks like this:
User generated image
I'm calling this in c#
 System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "genericName", "SendCSharpTempDataToJS(\'" + jSearializer.Serialize(_temperatureData) +"\');", true);

Open in new window


and then in my aspx page, i can successfully do this:
 function SendCSharpTempDataToJS(_temperatureData) {
            var mystring = "";
            console.log(_temperatureData);
        }

Open in new window


I now need to convert the object to the same type I had in the code behind, as i need to access x y data like [0,1] and retrieve the value 280

Any ideas how I would do that?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Your serializer is creating JSON - which will then be passed to the function as a string  - and then use it like this
 function SendCSharpTempDataToJS(_temperatureData) {
            var mystring = "";
            var data = JSON.parse(_temperatureData);
            console.log(data.x);
        }

Open in new window

Avatar of websss

ASKER

thanks, that comes in as "undefined"
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It appears your data is an array of values do you want to access a specific value or all values in the array?