2012-02-04 14 views
5

Ho i seguenti dati json provenienti dal servizio web che non è altro che un dizionario serializzato in json, ora sul lato client ho bisogno di analizzare questo indietro per scorrere le chiavi di questo json dizionario usando javascript o jqueryparsing json dictionary in javascript per iterare attraverso i tasti

{ 
   "AboutToExpire": { 
      "Display": true, 
      "Message": "Several of your subscriptions are about to expire. <a id=\"lnkShowExpiringSubs\" href=\"#\">View subscriptions<\/a>" 
   }, 
   "Expired": { 
      "Display": true, 
      "Message": "Your McAfee WaveSecure - Tablet Edition subscription has expired and you’ve been without protection for 384 days. <a id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\">Renew now<\/a>" 
   } 
} 
+0

per favore, farlo (i dati JSON) prima leggibile ?? – diEcho

+1

[Cosa hai provato?] (Http://mattgemmell.com/2008/12/08/what-have-you-tried/) Inoltre, qual è la tua domanda? – nfechner

risposta

5
var s = '{"AboutToExpire":{"Display":true,"Message":"Several of your subscriptions are about to expire. \u003ca id=\"lnkShowExpiringSubs\" href=\"#\"\u003eView subscriptions\u003c/a\u003e"},"Expired":{"Display":true,"Message":"Your McAfee WaveSecure - Tablet Edition subscription has expired and you’ve been without protection for 384 days. \u003ca id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\"\u003eRenew now\u003c/a\u003e"}}'; 

var data = eval(s); // this will convert your json string to a javascript object 

for (var key in data) { 
    if (data.hasOwnProperty(key)) { // this will check if key is owned by data object and not by any of it's ancestors 
     alert(key+': '+data[key]); // this will show each key with it's value 
    } 
} 
+1

'eval' è considerato non sicuro da un vasto pubblico. La risposta di @ shiplu.mokadd.im è l'opzione più sicura. – nonsensickle

+1

Valore in difetto per eval, si noti che questa è una cattiva idea sui dati esterni. – Jonathan

1

Analizzarlo? Si potrebbe semplicemente iterare su di esso come l'oggetto che è.

Dire che hai definito il tuo oggetto JSON come una variabile attraverso una chiusura o qualcosa, o per il gusto di esempio proprio come una variabile codificato ... IE:

var myJSON = "{"AboutToExpire":{"Display":true,"Message":"Several of your subscriptions are about to expire. \u003ca id=\"lnkShowExpiringSubs\" href=\"#\"\u003eView subscriptions\u003c/a\u003e"},"Expired":{"Display":true,"Message":"Your McAfee WaveSecure - Tablet Edition subscription has expired and you’ve been without protection for 384 days. \u003ca id=\"lnkNotificationRenewNow\" href=\"http://home.mcafee.com/root/campaign.aspx?cid=96035&pk=FAB37CF4-3680-4A87-A253-77E7D48BF6D7&affid=0\"\u003eRenew now\u003c/a\u003e"}}" 

con jQuery di ogni() si può solo iterare su di esso come.

$each(myJSON, function(x){document.print(myJSON.AboutToExpire[x].Message);`}); 
+0

Ho bisogno di controllare la chiave e fare qualche manipolazione per esempio se la chiave è AboutToExpire o qualcos'altro ho bisogno di applicare qualche classe e per altre chiavi altra classe sul valore che è Message in questo caso – almighty

+0

Inoltre myJSON.AboutToExpire sta arrivando come non definito – almighty

16

Usa JSON2.js

var obj = JSON.parse(data); 
for(var key in obj){ 
    if (obj.hasOwnProperty(key)){ 
     var value=obj[key]; 
     // work with key and value 
    } 
} 
+0

+1 per buona soluzione – diEcho

+1

Buona soluzione. Ma non è necessario includere JSON2.js a meno che non si guardino browser molto vecchi. Tutti i browser correnti hanno parse() e stringify() supportati in modo nativo. – techfoobar

+0

Non dovresti scambiare 'if (obj.hasOwnProperty (key)) {' e 'for (var key in obj) {' per ragioni di performance? – Neolisk