Reputation: 5439
If I do a post from jquery like so:
$.post("Row/getRowNames", { currRow: "MyRow", offset: 3 },
function(rowNames) {
/* How do I interpret the data to
},
"json");
How do I interpret the data rowNames that's passed back from the method in the controller? I need to turn the json result into an array of strings some how...
Here's the Controller method that gets the row names:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult getRowNames(string currRow, int offset)
{
return this.Json(_rowRepository.getRowNamesByOffset(currRow, offset));
}
and getRowNamesByOffset(currRow, offset)
returns an array of strings.
I'm really not sure how Json works, what is it doing to the array before it passes it back to the javascript? How is the javascript supposed to manipualte the Json in order to get the data it needs?
Upvotes: 1
Views: 1233
Reputation: 7514
JavaScript Object Notation (JSON) is returned and passed to the callback:
function(rowNames) {
// rowNames is an Array of JavaScript JSON Objects representing a RowName.
//
// Lets iterate over the rowNames. Let's let the RowName object have
// a Title property which we'll alert.
for (var i = 0; i < rowNames.length; i++) {
alert(rowNames[i].Title);
}
}
So basically the thing to note is that the parameter passed into the callback is a JSON object ... in this case a serialized array of RowNames
.
Hope this helps!
Upvotes: 1
Reputation: 421968
You don't need to do anything. JSON is native Javascript object representation. Just use the rowNames
in the function as if it where an array declared in Javascript.
To have the Javascript interpreter parse it, use eval
function passing the result as an argument. This can have some security consequences if you are getting the data from an untrusted source. For that matter, you could use some JSON parser Javascript libraries around to handle the parse task safely (as they won't execute possible Javascript statements inside as eval
does).
Upvotes: 2
Reputation: 16841
JSON stands for JavaScript Object Notation.
What that means is that the JSON code that's returned by the web server is converted to an object by JavaScript. You can use a JSON parser like this:
var myObject = JSON.parse(myJSONtext);
You can then reference all the properties returned directly through the object
alert(myObject.item1);
alert(myObject.item2);
Upvotes: 2
Reputation: 7942
You need a JSON parser, which you can get at the official JSON site. JSON builds comma delimited strings inside of brackets, braces and parentheses, which may be nested inside each other. JSON is tighter than XML but not very human readable.
Upvotes: -1
Reputation: 8494
If ever in doubt about the data being returned, alert() it (or console.log() it if running Firebug).
If it is an object of any description, for-in loop over it:
for (var i in yourResult) {
alert(i + " = " yourResult[i]);
}
This will show you the structure of what you're dealing with.
If yourResult is a JSON string and you want to turn it into JS, try:
yourResult = eval('(' + yourResult + ')');
Upvotes: 0