I'm fresh to this whole JSON and JQuery thing, and I'm trying to read a JSON structure coming from Delphi datasnap, e.g :
{"result":[[{"type":"VOMesas.TMesas","id":1,"fields":{ "FUsers":1,"FEnclosing":0,"FClientName":"","FCode":100,"FStatus":1,"FTotalValue":128.25}}]]}
How can I read it with JQuery, more especifically the Fields:{...} content ?
EDIT :
here is the function im trying to do
function getContent(order) {
$.getJSON("query.json",
function(data) {
$.each(data.result, function(i, item) {
var grid = '<table border="1">';
for (var i=0; i < item.length; i++){
CAMPO = item[i];
...
First: Let's assume you assigned your JSON object to a variable "myobject". Then you can do
Hope this code example will help you to understand the jQuery/JSON thing.
I have taken the sample JSON object as an array. Then populating small HTML by reading key/value pair of JSON.
working example: http://jsfiddle.net/ylokesh/WC84k/
If you're loading the data via
jQuery.ajax
or similar and it's being returned with the correct MIME type (or you telljQuery.ajax
that what you're getting back is JSON), then what you receive in thesuccess
callback will be a deserialized object (no longer JSON, but the objects that the JSON described). That being the case, you just access the properties of the object, e.g.:data
being the variable pointing to the object, which has aresult
property that's an array with only one entry (so, entry[0]
), which is itself another array with exactly one entry (so, entry[0]
again), which is an object with a property calledfields
. Pictorially:If you're retrieving the data some other way and it's still a string, you can deserialize it using
jQuery.parseJSON
:...and then do the above to access
fields
.If you've got a string of JSON, simply use
JSON.parse
to turn it into a Javascript object.By aware, however, that the JSON you've pasted into your question is invalid: