-1

Should be quite an easy one - I'm getting a dictionary in a .cs file and sending it back to .js as a json, using the following:

//return our value
return Response.AsJson(MyDictionary);

The .js file picks it up here:

App.Server.postJSON("/api/stuff/getmyresponse/" + self.id() + "/" + WhatToGet)
    .then(function (r) {
        console.log(r);
    });

This returns the following response, which is expected:

Object {3214368: 939000, 3214369: 701000, 3214370: 581000, $id: "100"}

(though the id isn't really necessary)

Now, what I'm wondering is, in the .js file, how do I start messing around with this data, or at least setting another variable to it and doing stuff like looping through and running some calculations?

domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
user25730
  • 517
  • 2
  • 6
  • 24
  • have u tried this: assign json to any var and use that variable for (i = 0; i < Object.length; i++) { text += Object[i] + "
    "; }
    – Nouman Bhatti Feb 28 '17 at 00:08
  • I can assign the json to another var, such as MyRes, but using MyRes.length or MyRes[0] returns undefined. – user25730 Feb 28 '17 at 00:21

2 Answers2

0

You already have what you need in the 'r' object:

App.Server.postJSON("/api/stuff/getmyresponse/" + self.id() + "/" + WhatToGet) .then(function (r) { console.log(r.3214368); // 939000 console.log(r.3214370); // 581000 });

toxicafunk
  • 396
  • 2
  • 7
0

This seems to do the job somewhat:

var MyRes;
App.Server.postJSON("/api/stuff/getmyresponse/" + self.id() + "/" + WhatToGet)
.then(function (r) {
    MyRes = r;
    for(i in MyRes)
    {
        console.log(i);
        console.log(MyRes[i]);
    }
});
user25730
  • 517
  • 2
  • 6
  • 24