-1

以下の Json データを ajax 応答で受け取ります...それをトラバースする方法がわかりません。それをループして、配列を作成します。これを HTML DOM で使用することも、このデータをキャッシュするために使用します。データがすでに利用可能な場合、Ajax リクエストはもう必要ありません...

$.each(data, function(key,value){ ---- }) でそれをやろうとしました。しかし、問題は、forループを入れたいデータをループしていて、その中に他の配列またはオブジェクトを入力したい...

それは意味がありますか、私たちはそれを行うことができますか?

{"CountryCode":"ABW","Language":"Dutch"}{"CountryCode":"ABW","Language":"English"}{"CountryCode":"ABW","Language":"Papiamento"}{"CountryCode":"ABW","Language":"Spanish"}{"CountryCode":"AFG","Language":"Balochi"}{"CountryCode":"AFG","Language":"Dari"}{"CountryCode":"AFG","Language":"Pashto"}{"CountryCode":"AFG","Language":"Turkmenian"}{"CountryCode":"AFG","Language":"Uzbek"}{"CountryCode":"AGO","Language":"Ambo"}{"CountryCode":"AGO","Language":"Chokwe"}{"CountryCode":"AGO","Language":"Kongo"}{"CountryCode":"AGO","Language":"Luchazi"}{"CountryCode":"AGO","Language":"Luimbe-nganguela"}{"CountryCode":"AGO","Language":"Luvale"}{"CountryCode":"AGO","Language":"Mbundu"}{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"}{"CountryCode":"AGO","Language":"Ovimbundu"}{"CountryCode":"AIA","Language":"English"}{"CountryCode":"ALB","Language":"Albaniana"}{"CountryCode":"ALB","Language":"Greek"}{"CountryCode":"ALB","Language":"Macedonian"}{"CountryCode":"AND","Language":"Catalan"}{"CountryCode":"AND","Language":"French"}{"CountryCode":"AND","Language":"Portuguese"}{"CountryCode":"AND","Language":"Spanish"}{"CountryCode":"ANT","Language":"Dutch"}{"CountryCode":"ANT","Language":"English"}{"CountryCode":"ANT","Language":"Papiamento"}{"CountryCode":"ARE","Language":"Arabic"}{"CountryCode":"ARE","Language":"Hindi"}{"CountryCode":"ARG","Language":"Indian Languages"}{"CountryCode":"ARG","Language":"Italian"}{"CountryCode":"ARG","Language":"Spanish"}{"CountryCode":"ARM","Language":"Armenian"}{"CountryCode":"ARM","Language":"Azerbaijani"}{"CountryCode":"ASM","Language":"English"}{"CountryCode":"ASM","Language":"Samoan"}{"CountryCode":"ASM","Language":"Tongan"}{"CountryCode":"ATG","Language":"Creole English"}{"CountryCode":"ATG","Language":"English"}{"CountryCode":"AUS","Language":"Arabic"}{"CountryCode":"AUS","Language":"Canton Chinese"}{"CountryCode":"AUS","Language":"English"}{"CountryCode":"AUS","Language":"German"}{"CountryCode":"AUS","Language":"Greek"}{"CountryCode":"AUS","Language":"Italian"}{"CountryCode":"AUS","Language":"Serbo-Croatian"}{"CountryCode":"AUS","Language":"Vietnamese"}{"CountryCode":"AUT","Language":"Czech"}{"CountryCode":"AUT","Language":"German"}{"CountryCode":"AUT","Language":"Hungarian"}{"CountryCode":"AUT","Language":"Polish"}{"CountryCode":"AUT","Language":"Romanian"}{"CountryCode":"AUT","Language":"Serbo-Croatian"}{"CountryCode":"AUT","Language":"Slovene"}{"CountryCode":"AUT","Language":"Turkish"}{"CountryCode":"AZE","Language":"Armenian"}{"CountryCode":"AZE","Language":"Azerbaijani"}{"CountryCode":"AZE","Language":"Lezgian"}{"CountryCode":"AZE","Language":"Russian"}{"CountryCode":"BDI","Language":"French"}{"CountryCode":"BDI","Language":"Kirundi"}{"CountryCode":"BDI","Language":"Swahili"}{"CountryCode":"BEL","Language":"Arabic"}{"CountryCode":"BEL","Language":"Dutch"}{"CountryCode":"BEL","Language":"French"}{"CountryCode":"BEL","Language":"German"}{"CountryCode":"BEL","Language":"Italian"}{"CountryCode":"BEL","Language":"Turkish"}{"CountryCode":"BEN","Language":"Adja"}{"CountryCode":"BEN","Language":"Aizo"}
4

2 に答える 2

1

問題は次の 2 つの手順で解決できます。

1) json データを php 文字列に変換します 2) その配列を反復処理します。

ステップ1)配列に変換

$arr = json_decode($jsondata);

ステップ 2) 配列を反復処理する

arrayRecurssive($arr);

function arrayRecurssive($arr){

    foreach($arr as $key=>$val){
        if(is_array($val)){
            echo "$key\n";
            arrayRecurssive($val);
        }else{
           echo "$key\n";
           echo "$val\n";
        }

    }
}

上記の例では、新しい行として '\n' を使用していますが、<br/>. 出力のさらなるフォーマットは、コーダーの演習として残されています:-)

于 2013-02-13T06:50:08.133 に答える
1

json データの形式が正しくないようです。そのはず:

[{"CountryCode":"ABW","Language":"Dutch"},{"CountryCode":"ABW","Language":"English"},{"CountryCode":"ABW","Language":"Papiamento"},{"CountryCode":"ABW","Language":"Spanish"},{"CountryCode":"AFG","Language":"Balochi"},{"CountryCode":"AFG","Language":"Dari"},{"CountryCode":"AFG","Language":"Pashto"},{"CountryCode":"AFG","Language":"Turkmenian"},{"CountryCode":"AFG","Language":"Uzbek"},{"CountryCode":"AGO","Language":"Ambo"},{"CountryCode":"AGO","Language":"Chokwe"},{"CountryCode":"AGO","Language":"Kongo"},{"CountryCode":"AGO","Language":"Luchazi"},{"CountryCode":"AGO","Language":"Luimbe-nganguela"},{"CountryCode":"AGO","Language":"Luvale"},{"CountryCode":"AGO","Language":"Mbundu"},{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"},{"CountryCode":"AGO","Language":"Ovimbundu"},{"CountryCode":"AIA","Language":"English"},{"CountryCode":"ALB","Language":"Albaniana"},{"CountryCode":"ALB","Language":"Greek"},{"CountryCode":"ALB","Language":"Macedonian"},{"CountryCode":"AND","Language":"Catalan"},{"CountryCode":"AND","Language":"French"},{"CountryCode":"AND","Language":"Portuguese"},{"CountryCode":"AND","Language":"Spanish"},{"CountryCode":"ANT","Language":"Dutch"},{"CountryCode":"ANT","Language":"English"},{"CountryCode":"ANT","Language":"Papiamento"},{"CountryCode":"ARE","Language":"Arabic"},{"CountryCode":"ARE","Language":"Hindi"},{"CountryCode":"ARG","Language":"Indian Languages"},{"CountryCode":"ARG","Language":"Italian"},{"CountryCode":"ARG","Language":"Spanish"},{"CountryCode":"ARM","Language":"Armenian"},{"CountryCode":"ARM","Language":"Azerbaijani"},{"CountryCode":"ASM","Language":"English"},{"CountryCode":"ASM","Language":"Samoan"},{"CountryCode":"ASM","Language":"Tongan"},{"CountryCode":"ATG","Language":"Creole English"},{"CountryCode":"ATG","Language":"English"},{"CountryCode":"AUS","Language":"Arabic"},{"CountryCode":"AUS","Language":"Canton Chinese"},{"CountryCode":"AUS","Language":"English"},{"CountryCode":"AUS","Language":"German"},{"CountryCode":"AUS","Language":"Greek"},{"CountryCode":"AUS","Language":"Italian"},{"CountryCode":"AUS","Language":"Serbo-Croatian"},{"CountryCode":"AUS","Language":"Vietnamese"},{"CountryCode":"AUT","Language":"Czech"},{"CountryCode":"AUT","Language":"German"},{"CountryCode":"AUT","Language":"Hungarian"},{"CountryCode":"AUT","Language":"Polish"},{"CountryCode":"AUT","Language":"Romanian"},{"CountryCode":"AUT","Language":"Serbo-Croatian"},{"CountryCode":"AUT","Language":"Slovene"},{"CountryCode":"AUT","Language":"Turkish"},{"CountryCode":"AZE","Language":"Armenian"},{"CountryCode":"AZE","Language":"Azerbaijani"},{"CountryCode":"AZE","Language":"Lezgian"},{"CountryCode":"AZE","Language":"Russian"},{"CountryCode":"BDI","Language":"French"},{"CountryCode":"BDI","Language":"Kirundi"},{"CountryCode":"BDI","Language":"Swahili"},{"CountryCode":"BEL","Language":"Arabic"},{"CountryCode":"BEL","Language":"Dutch"},{"CountryCode":"BEL","Language":"French"},{"CountryCode":"BEL","Language":"German"},{"CountryCode":"BEL","Language":"Italian"},{"CountryCode":"BEL","Language":"Turkish"},{"CountryCode":"BEN","Language":"Adja"},{"CountryCode":"BEN","Language":"Aizo"}]

次に、それを配列に割り当てるだけです。例えば:

var countries = $.parseJSON('[{"CountryCode":"ABW","Language":"Dutch"},{"CountryCode":"ABW","Language":"English"},{"CountryCode": "ABW","Language":"パピアメント"},{"CountryCode":"ABW","Language":"Spanish"},{"CountryCode":"AFG","Language":"Balochi"},{ "CountryCode":"AFG","Language":"Dari"},{"CountryCode":"AFG","Language":"Pashto"},{"CountryCode":"AFG","Language":"トルクメニア語"},{"CountryCode":"AFG","Language":"ウズベク語"},{"CountryCode":"AGO","Language":"Ambo"},{"CountryCode":"AGO","Language":"Chokwe"},{"CountryCode":"AGO","Language":"Kongo"},{"CountryCode":"AGO","Language":"ルチャジ"},{"CountryCode":"AGO","Language":"Luimbe-nganguela"},{"CountryCode":"AGO","Language":"Luvale"},{"CountryCode":"AGO" ,"Language":"Mbundu"},{"CountryCode":"AGO","Language":"Nyaneka-nkhumbi"},{"CountryCode":"AGO","Language":"Ovimbundu"},{" CountryCode":"AIA","Language":"English"},{"CountryCode":"ALB","Language":"アルバニア"},{"CountryCode":"ALB","Language":"ギリシャ語"},{"CountryCode":"ALB","Language":"マケドニア語"},{"CountryCode":"AND","Language":"カタロニア語" },{"CountryCode":"AND","Language":"French"},{"CountryCode":"AND","Language":"Portuguese"},{"CountryCode":"AND","Language" :"スペイン語"},{"国コード":"ANT","言語":"オランダ語"},{"国コード":"ANT","言語":"英語"},{"国コード":"ANT" ,"言語":"パピアメント"},{"国コード":"ARE","言語":"アラビア語"},{"国コード":"ARE","Language":"Hindi"},{"CountryCode":"ARG","Language":"Indian Languages"},{"CountryCode":"ARG","Language":"Italian"},{ "CountryCode":"ARG","Language":"Spanish"},{"CountryCode":"ARM","Language":"Armenian"},{"CountryCode":"ARM","Language":"アゼルバイジャン語"},{"CountryCode":"ASM","Language":"English"},{"CountryCode":"ASM","Language":"Samoan"},{"CountryCode":"ASM","Language ":"トンガ"},{"国コード":"ATG","言語":"クレオール英語"},{"国コード":"ATG","Language":"English"},{"CountryCode":"AUS","Language":"Arabic"},{"CountryCode":"AUS","Language":"Canton Chinese"},{"CountryCode" :"AUS","Language":"English"},{"CountryCode":"AUS","Language":"German"},{"CountryCode":"AUS","Language":"Greek"}, {"CountryCode":"AUS","Language":"Italian"},{"CountryCode":"AUS","Language":"セルビア クロアチア語"},{"CountryCode":"AUS","Language" :"ベトナム語"},{"CountryCode":"AUT","言語":"チェコ語"},{"CountryCode":"AUT","Language":"ドイツ語"},{"CountryCode":"AUT","Language":"ハンガリー語"},{"CountryCode":"AUT","Language":"ポーランド語"},{"CountryCode":" AUT","Language":"ルーマニア語"},{"CountryCode":"AUT","Language":"セルビア クロアチア語"},{"CountryCode":"AUT","Language":"スロベニア語"}, {"CountryCode":"AUT","Language":"トルコ語"},{"CountryCode":"AZE","Language":"アルメニア語"},{"CountryCode":"AZE","Language":"アゼルバイジャン語"},{"国コード":"AZE","言語":"レズ語"},{"国コード":"AZE","言語":"ロシア語"},{"CountryCode":"BDI","Language":"フランス語"},{"CountryCode":"BDI","Language":"Kirundi"},{"CountryCode":"BDI", "Language":"Swahili"},{"CountryCode":"BEL","Language":"Arabic"},{"CountryCode":"BEL","Language":"Dutch"},{"CountryCode": "BEL","Language":"French"},{"CountryCode":"BEL","Language":"German"},{"CountryCode":"BEL","Language":"Italian"},{ "CountryCode":"BEL","Language":"トルコ語"},{"CountryCode":"BEN","Language":"Adja"},{"CountryCode":"BEN","Language":"愛蔵"}]');

アラート (countries.length);

于 2013-02-13T06:50:52.780 に答える