如何使用JQuery阅读JSON结果中的Fields:{}内容? [英] How to read the Fields:{} content inside a JSON result using JQuery?

查看:174
本文介绍了如何使用JQuery阅读JSON结果中的Fields:{}内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这整个JSON和JQuery很新鲜,我正在尝试阅读来自Delphi datasnap的JSON结构,例如:

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

我如何用JQuery阅读它,更具体地说是字段:{...}内容?

How can I read it with JQuery, more especifically the 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];

            ...


推荐答案

如果你'通过 jQuery.ajax 或类似的方式重新加载数据,它是b eing返回正确的MIME类型(或者告诉 jQuery.ajax 你得到的是JSON),然后你在中收到的内容成功回调将是一个反序列化的对象(不再是JSON,而是JSON描述的对象)。在这种情况下,您只需访问对象的属性,例如:

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 是指向对象的变量,它有一个结果属性,这是一个只有一个条目的数组(所以,条目 [0] ),这是本身是另一个只有一个条目的数组(因此,再次输入 [0] ),这是一个具有名为的属性的对象。图示:

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

如果您正在以其他方式检索数据并且它仍然是字符串,则可以使用 <对其进行反序列化。 code> JQ uery.parseJSON

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.

这篇关于如何使用JQuery阅读JSON结果中的Fields:{}内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆