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

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
websssCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Have you check your indices? Not that you're off by one..
websssCEOAuthor Commented:
Yes, I did check the surrounding values, it's not the issue
leakim971PluritechnicianCommented:
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

Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

websssCEOAuthor 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
Kelvin McDanielSr. DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
websssCEOAuthor Commented:
thanks all
Kelvin, this was the bit that got it working
@Html.Raw(ViewBag.TempDataArray);
websssCEOAuthor 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?
websssCEOAuthor Commented:
Kelvin McDanielSr. DeveloperCommented:
I commented there, requesting your Controller method code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.