-1

我在 ajax 响应中收到以下 Json 数据...我不知道如何遍历它我想遍历它并从中制作一个数组,我将在 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

您的问题可以分两步解决:

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":"Papiamento"},{"CountryCode":"ABW","Language":"Spanish"},{"CountryCode":"AFG","Language":"Balochi"},{ "CountryCode":"AFG","Language":"Dari"},{"CountryCode":"AFG","Language":"Pashto"},{"CountryCode":"AFG","Language":"土库曼语"},{"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":"葡萄牙语"},{"CountryCode":"AND","Language" :"Spanish"},{"CountryCode":"ANT","Language":"Dutch"},{"CountryCode":"ANT","Language":"English"},{"CountryCode":"ANT" ,"Language":"Papiamento"},{"CountryCode":"ARE","Language":"阿拉伯语"},{"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":"克里奥尔英语"},{"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" :"越南语"},{"CountryCode":"AUT","Language":"捷克语"},{"CountryCode":"AUT","Language":"German"},{"CountryCode":"AUT","Language":"Hungarian"},{"CountryCode":"AUT","Language":"Polish"},{"CountryCode":" AUT","Language":"罗马尼亚语"},{"CountryCode":"AUT","Language":"Serbo-Croatian"},{"CountryCode":"AUT","Language":"Slovene"}, {"CountryCode":"AUT","Language":"Turkish"},{"CountryCode":"AZE","Language":"Armenian"},{"CountryCode":"AZE","Language":"阿塞拜疆语"},{"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"}]');

警报(国家。长度);

于 2013-02-13T06:50:52.780 回答