Domanda

Sto usando titanio per un'applicazione mobile. Nell'applicazione, il server restituisce dati JSON, che viene poi analizzati da JSON.parse (). Su Android, funziona benissimo. Ho anche controllato due volte per assicurarsi che sia valido con http://jsonformatter.curiousconcept.com/

Ecco i miei dati JSON:

    {
   "email":"example@mail.com",
   "count":6,
   "0":{
      "id":"146996",
      "user_id":"25069",
      "item":"item1",
      "start_my_day":"none",
      "scheduled":"n",
      "calendar":"none",
      "start":"00000000T000000",
      "end":"00000000T000000",
      "added":"2011-11-30 06:55:47",
      "updated":"2011-11-30 06:55:47"
   },
   "1":{
      "id":"146988",
      "user_id":"25069",
      "item":"item2",
      "start_my_day":"none",
      "scheduled":"n",
      "calendar":"none",
      "start":"00000000T000000",
      "end":"00000000T000000",
      "added":"2011-11-30 06:52:20",
      "updated":"2011-11-30 06:52:20"
   }
   }

Quando ho cercato di controllare ciò che ottengo con:

var response = JSON.parse(json, function (key, value) {
    Ti.API.debug('JSON: ' + key + ' <-> ' + value);
    return value;
});

sembra che l'oggetto "0" non viene analizzato come dovrebbe essere, ma i suoi campi sono fatti parte del genitore. Ecco l'output:

[DEBUG] JSON: email <-> example@mail.com
[DEBUG] JSON: count <-> 2
[DEBUG] JSON: id <-> 146996
[DEBUG] JSON: user_id <-> 25069
[DEBUG] JSON: item <-> item1
[DEBUG] JSON: start_my_day <-> none
[DEBUG] JSON: scheduled <-> n
[DEBUG] JSON: calendar <-> none
[DEBUG] JSON: start <-> 00000000T000000
[DEBUG] JSON: end <-> 00000000T000000
[DEBUG] JSON: added <-> 2011-11-30 06:55:47
[DEBUG] JSON: updated <-> 2011-11-30 06:55:47
[DEBUG] JSON: 0 <-> [object Object]
[DEBUG] JSON: id <-> 146988
[DEBUG] JSON: user_id <-> 25069
[DEBUG] JSON: item <-> item2
[DEBUG] JSON: start_my_day <-> none
[DEBUG] JSON: scheduled <-> n
[DEBUG] JSON: calendar <-> none
[DEBUG] JSON: start <-> 00000000T000000
[DEBUG] JSON: end <-> 00000000T000000
[DEBUG] JSON: added <-> 2011-11-30 06:52:20
[DEBUG] JSON: updated <-> 2011-11-30 06:52:20
[DEBUG] JSON: 1 <-> [object Object]
[DEBUG] JSON:  <-> [object Object]

Da quello che vedo .. non è quello che si suppone di tornare. Ho cercato di racchiudere il conteggio tra virgolette, per cambiare il "0" a "10", ma i soggiorni di analisi lo stesso. Se avete bisogno di maggiori informazioni, per favore fatemelo sapere.

Grazie

È stato utile?

Soluzione

JSON.parse(str, func) is called for each property recursively for formatting/replacement purposes. It is not failing, but you should not use it for your needs.

If you want to iterate over the object, you'd better parse the JSON regularly and use a loop:

var parsed = JSON.parse(json);

for(var key in parsed) {
    console.log(key, parsed[key]);

    for(var key2 in parsed[key]) {
        console.log("Nested: ", key2, parsed[key][key2]);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top