Float array in c# - when passed to JS it's in wrong order

websss
websss used Ask the Experts™
on
Hi, I have the float array which is populated in c#

temperatureData= new float[570, 429];

Open in new window



I then prepare the data in c# to be send to the front end by doing the following (this might be where its going wrong)

System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
jSearializer.MaxJsonLength = int.MaxValue;
pd.TemperatureDataArray = jSearializer.Serialize(temperatureData);

ViewBag.TempDataArray = pd.TemperatureDataArray;

Open in new window



then in the front end i have

var tempDataArray = @(ViewBag.TempDataArray);


var imageTemperatureDataArray = JSON.stringify(tempDataArray);
imageTemperatureDataArray = JSON.parse(imageTemperatureDataArray);


alert(imageTemperatureDataArray[321, 228]);

Open in new window


However the value in position 321,228 in JS is not the same as in C#, so i assume its re-ordered the data some how


Can anyone advise where its getting muddled up?


I've attached the dataset which is was taken on a BP on this line
ViewBag.TempDataArray = pd.TemperatureDataArray


The correct value in the float array at this position  [321, 228] should be 346.795624
However in the front end i'm seeing 304.92453
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
Have you check your indices? Not that you're off by one..
websssCEO

Author

Commented:
Yes, I did check the surrounding values, it's not the issue
leakim971Multitechnician
Top Expert 2014
Commented:
two dimensional array don't really exists in JavaScript

when you can do :
arr[321, 228] = 346.795624;

in JavaScript you do :
a[321][228] = 346.795624;

and get a JSON object like this :
{321:{228:346.795624}}

why the following lines ? seems to be useless for me.
var imageTemperatureDataArray = JSON.stringify(tempDataArray); // create a string from an object/JSON(?)
imageTemperatureDataArray = JSON.parse(imageTemperatureDataArray); create an object from a JSON string


what about :
var imageTemperatureDataArray = @(ViewBag.TempDataArray);
alert( imageTemperatureDataArray[321][228] );

Open in new window


if that don't work please post what you get in the alert with :
var imageTemperatureDataArray = @(ViewBag.TempDataArray);
alert( JSON.stringify(imageTemperatureDataArray) );

Open in new window


or in the browser console developer tools with :
var imageTemperatureDataArray = @(ViewBag.TempDataArray);
console.log( JSON.stringify(imageTemperatureDataArray) );

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

websssCEO

Author

Commented:
Thanks

I implemented what you said

On this line i get UNDEFINED
alert(imageTemperatureDataArray[321][228]);

Open in new window


the console.write output is in the attached file
consolewrite.txt
Agree with @leakim971 that the extra serialization isn't necessary. Try this instead:

var imageTemperatureDataArray = @Html.Raw(ViewBag.TempDataArray);
alert(imageTemperatureDataArray[321][228]);

Open in new window

websssCEO

Author

Commented:
thanks all
Kelvin, this was the bit that got it working
@Html.Raw(ViewBag.TempDataArray);
websssCEO

Author

Commented:
Hey guys, sorry for bringing this up again, but I have a slightly different issue

I've also accessed this float array from a ajax url.action Post
In the CS i return an object in JSONResult
However its a javascript variable at this point and cannot run @Html.Raw(ViewBag.TempDataArray);
its coming back as
[[273.7,273.8,273.8,274.6,274.4 etc

And I need to convert it to array as this did: @Html.Raw(ViewBag.TempDataArray);

Any ideas?
I commented there, requesting your Controller method code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial