2012-02-04 13 views
5

私は以下のjsonデータをWebサービスから取得していますが、これはjsonにシリアル化された辞書です。クライアント側でこれを解析してこのjsonのキーを反復する必要があります辞書を使用してjavacriptまたはjqueryJavaScriptを使ってjson辞書を解析してキーを反復する

{ 
   "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

あなたがそれ(JSONデータ)読める最初にしてくださいます? – diEcho

+1

[何を試しましたか?](http://mattgemmell.com/2008/12/08/what-have-you-tried/)また、あなたの質問は何ですか? – nfechner

答えて

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'は幅広い聴衆には安全でないとみなされます。 @ shiplu.mokadd.imの答えはより安全なオプションです。 – nonsensickle

+1

evalのためのDownvote、これは外部データの悪い考えであることに注意してください。 – Jonathan

1

構文解析するには?あなたはそれをオブジェクトとして反復することができます。各()あなただけできるjQueryので

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

は...あなたが閉鎖か何かを介して可変としてあなたのJSONオブジェクトを定義し、または変数がハードコードと同じように、例示のためIEと言いますそれを反復する。

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

私はキーを確認し、キーがAboutToExpireかどうかなどいくつかの操作を行う必要があります。何らかのクラスを適用する必要があり、他のキーの値に他のクラスの値がある場合 – almighty

+0

さらにmyJSON.AboutToExpireが来ています定義されていないので – almighty

16

使用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良い解決策のため – diEcho

+1

良い解決策。しかし、非常に古いブラウザを見ていない限り、JSON2.jsをインクルードする必要はありません。現在のブラウザーはすべて、ネイティブでサポートされているparse()とstringify()を持っています。 – techfoobar

+0

パフォーマンス上の理由から 'if(obj.hasOwnProperty(key)){'と 'for(objのvarキー){'を入れ替えてはいけませんか? – Neolisk

関連する問題