Domanda

Hey guys! Prima di tutto, so che questo genere di cose è abbastanza merda, ma questo è il modo in cui visualizzo raggiungere il mio obiettivo.

Qui è l'affare: Ho un oggetto JSON che contiene un array, ma purtroppo, devo scorrere l'elenco di un oggetto JSON, e poi iterare throught tale matrice ho mencioned prima. L'oggetto JSON sguardo come segue (è in portoghese, ma voi ragazzi può capire):

{ "Produtos": [{ "chiave": "DERIVADOS de Ferro", "Value": 217816909}, { "chiave": "MADEIRAS, SUAS MANUFATURAS E Mobiliario MÉDICO CIRÚRGICO " "Value": 117812290}, { "chiave":" CELULOSE, PAPEL E SUAS OBRAS " "Value": 100086937}, { "chiave":" CARNE Suina " "Value": 81738783}, { "chiave":" CARNE BOVINA " "Value": 74894768}, { "chiave":" Carne de AVES", "valore": 65292433}, { "chiave": "Outros", "Value": 444.520.811}], "ano": 2005, "tipo": 1}

Produtos è la matrice ho citato, e ho altri due valori importanti: ano (anno) e tipo (tipo). Il mio codice sguardo come segue:

jQuery.getJSON("/webportos/Porto/GraficoComercio?id=" + iddoporto.className + "&ano=2005", null, function (items) {                
    jQuery.each(items, function (itemNo, item) {
        jQuery.each(item.produtos, function (dicNo, dic) {
            options.series.push({ name: dic.Key, data: dic.Value});                             
            options.title.text = "Comércio - "+item.ano;
            options.yAxis.title.text = "Exportação";                                
            options.tooltip.formatter = function () {
                return '<b><u>' + this.series.name + '</u></b><br/><b>' + this.x + '</b>: ' + this.y + ' ';
            }           
        });
    });

    chart = new Highcharts.Chart(options);              
});

Questo pezzo di codice rende console Chrome JavaScript per visualizzare:

ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js:32 Uncaught TypeError: Cannot read property 'length' of undefined

Puoi ragazzi darmi una mano?

È stato utile?

Soluzione

E 'questo perché si sta cercando di utilizzare .each () su un non array?

jQuery.each(items, function (itemNo, item) {

Dalla mia comprensione, jQuery.each (oggetto, callback, args) è solo per l'uso su array. La prima cosa che fa è effettuare una chiamata a object.length. Quindi, se non si ha

[ { "Produtos": [{ "chiave": "DERIVADOS de Ferro", "Value": 217816909}, { "chiave": "MADEIRAS, Suas MANUFATURAS E Mobiliario MÉDICO CIRÚRGICO " "Value": 117812290}, { "chiave":" CELULOSE, PAPEL E SUAS OBRAS " "Value": 100086937}, { "chiave": "CARNE suina", "Value": 81738783}, { "chiave":" CARNE BOVINA", "Value": 74894768}, { "chiave": "Carne de AVES", "Value": 65292433}, { "Chiave": "Outros", "Value": 444520811}], "ano": 2005, "tipo": 1} ]

che ti capita questo errore.

Inoltre, si può prendere il suggerimento di Diodeus in considerazione. jQuery.each è un po 'meno performante di un semplice utilizzando una per per scorrere i dati.

Altri suggerimenti

Non è necessario jQuery per scorrere i dati JSON. Utilizzare pianura vecchio Javascript.

Vai a questa domanda: display JSON / gerarchia YAML come un albero in HTML?

function renderJSON(obj) {
    var keys = []
    var retValue = ""
    for (var key in obj) {
            if(typeof obj[key] == 'object') {
                    retValue += "<div class='tree'>" + key                                          
                    retValue += renderJSON(obj[key])
                    retValue += "</div>"
            }
            else {
                    retValue += "<div class='tree'>" + key + " = " + obj[key] + "</div>"
            }

       keys.push(key)
    }
    return retValue
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top