Showing content of JSON result and 'undefined' in a grid

狂风中的少年 提交于 2020-01-06 18:09:42

问题


So, I'm using JQuery to read a JSON and put it inside a grid. Problem is : the grid won't show when the JSON is :

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

But if the JSON is like that, without the field:{...} :

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

It reads.

Here is the function I'm using:

function getContent(order) {
    $.getJSON("query.json", function(data) {
        $.each(data.result, function(i, item) {

            var grid = '<table border="1">';

            var i=0;

            var CAMPO;

            for (i=0; i < item.length; i++){
                CAMPO = item[i];


                grid += '<tr><td>'+ CAMPO.FQtdPessoas +
                        '</td><td>'+ CAMPO.FDataFechamento +
                        '</td><td>'+ CAMPO.FTempoPermanencia +
                        '</td><td>'+ CAMPO.FNomeCliente +
                        '</td><td>'+ CAMPO.FValorAcrescimo +
                        '</td><td>'+ CAMPO.FValorDesconto +
                        '</td><td>'+ CAMPO.FValorServico +
                        '</td><td>'+ CAMPO.FDataAbertura +
                        '</td><td>'+ CAMPO.FNumero +
                        '</td><td>'+ CAMPO.FStatus +
                        '</td><td>'+ CAMPO.FValorTotal +
                        '</td><td>'+ CAMPO.FTerminalAberto +
                        '</td><td>'+ CAMPO.FNumeroVenda +
                        '</td><td>'+ CAMPO.FGarcon +
                        '</td></tr>';
            }
            grid += '</table>'; 

            $("#dvContent").html(grid);
            $("#dvContent").find("a").click(function(e){
                var link = $(this);
                getContent(link.html());
            });
        });
    });
}

$(document).ready(function(){
$(function() {
getContent();
});
});

回答1:


Looks like the problem is in the code you're omitting:

grid += '<tr><td>'+ ........ +'</td></tr>';

If the json result is in the first format, then you should be able to access the fields object using CAMPO.fields.

Edit:

Example:

grid += '<tr><td>'+ CAMPO.fields.FQtdPessoas +'</td><td>'+ CAMPO.fields.FDataFechamento +'</td><td>'+ [...]



回答2:


For using properties under field use:

item.result[0][0].fields.<<Your proprerties here>>

For the Id and Type do:

item.result[0][0].id
item.result[0][0].type

Edit:

for (i = 0; i < item.result.length; i++) {
            CAMPO = item.result[i][i].fields;
             grid += '<tr><td>'+ CAMPO.FQtdPessoas +
                    '</td><td>'+ CAMPO.FDataFechamento +'</td></tr>';
}



回答3:


In your code, CAMPO refers to the object that looks like this:

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

To access its fields, use the fields property (or set CAMPO to item[i].fields).



来源:https://stackoverflow.com/questions/9180622/showing-content-of-json-result-and-undefined-in-a-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!