How to read the Fields:{} content inside a JSON re

2019-06-13 15:25发布

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];

            ...

4条回答
何必那么认真
2楼-- · 2019-06-13 15:49

First: Let's assume you assigned your JSON object to a variable "myobject". Then you can do

var myfields = myobject.result[0][0].fields;
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-13 15:51

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/

查看更多
聊天终结者
4楼-- · 2019-06-13 15:55

If you're loading the data via jQuery.ajax or similar and it's being returned with the correct MIME type (or you tell jQuery.ajax that what you're getting back is JSON), then what you receive in the success 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.:

$.ajax({
    // ...
    success: function(data) {
        var fields = data.result[0][0].fields;
    }
});

data being the variable pointing to the object, which has a result 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 called fields. Pictorially:

{                                        // <== data
    "result": [                          // <== data.result
        [                                // <== data.result[0]
            {                            // <== data.result[0][0]
                "type": "VOMesas.TMesas",
                "id": 1,
                "fields": {              // <== data.result[0][0].fields
                    "FUsers": 1,
                    "FEnclosing": 0,
                    "FClientName": "",
                    "FCode": 100,
                    "FStatus": 1,
                    "FTotalValue": 128.25
                }
            }
        ]
    ]
}

If you're retrieving the data some other way and it's still a string, you can deserialize it using jQuery.parseJSON:

var data = $.parseJSON(str);

...and then do the above to access fields.

查看更多
forever°为你锁心
5楼-- · 2019-06-13 16:11

If you've got a string of JSON, simply use JSON.parse to turn it into a Javascript object.

var datasnap = '{"result":[[{"type":"VOMesas.TMesas","id":1,"fields": FUsers":1,"FEnclosing:0,"FClientName":"","FCode":100,"FStatus":1,"FTotalValue":128.25}}]]}';
var data = JSON.parse(datasnap);
var fields = data['result'][0]['fields'];

By aware, however, that the JSON you've pasted into your question is invalid:

{
    "result": [
        [
            {
                "type": "VOMesas.TMesas",
                "id": 1,
                "fields": FUsers":1,"FEnclosing: 0, //unbalanced "
                "FClientName": "",
                "FCode": 100,
                "FStatus": 1,
                "FTotalValue": 128.25
            }
        } //unbalanced }
    ]
]
}
查看更多
登录 后发表回答