如何使用 JQuery 读取 JSON 结果中的 Fields:{} 内容?

发布于 2025-01-03 02:32:47 字数 628 浏览 0 评论 0原文

我对整个 JSON 和 JQuery 的事情很陌生,我正在尝试读取来自 Delphi datasnap 的 JSON 结构,例如:

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

如何使用 JQuery 读取它,更具体地说是 Fields:{...} content ?

编辑:

这是我想做的功能

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

            ...

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

            ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怂人 2025-01-10 02:32:47

如果您通过 jQuery.ajax 或类似方式加载数据,并且它以正确的 MIME 类型返回(或者您告诉 jQuery.ajax 您要返回的内容)是 JSON),那么您在 success 回调中收到的将是一个反序列化的对象(不再是 JSON,而是 JSON 描述的对象)。在这种情况下,您只需访问对象的属性,例如:

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

data 是指向该对象的变量,它有一个 result 属性,该属性是一个只有一个的数组条目(所以,条目[0]),它本身是另一个只有一个条目的数组(所以,又是条目[0]),它是一个具有属性的对象称为字段。如图所示:

{                                        // <== 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
                }
            }
        ]
    ]
}

如果您以其他方式检索数据并且它仍然是一个字符串,您可以使用 jQuery.parseJSON:

var data = $.parseJSON(str);

...然后执行上述操作来访问字段

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.

一紙繁鸢 2025-01-10 02:32:47

如果您有 JSON 字符串,只需使用 JSON.parse 将其转换为 Javascript 对象即可。

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

但是,请注意,您粘贴到问题中的 JSON 无效:

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

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 }
    ]
]
}
无畏 2025-01-10 02:32:47

首先:假设您将 JSON 对象分配给变量“myobject”。然后你可以做

var myfields = myobject.result[0][0].fields;

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

var myfields = myobject.result[0][0].fields;
安人多梦 2025-01-10 02:32:47

希望这个代码示例能够帮助您理解 jQuery/JSON 的东西。

我已将示例 JSON 对象作为数组。然后通过读取 JSON 的键/值对来填充小 HTML。

工作示例: http://jsfiddle.net/ylokesh/WC84k/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文