我在ajax响应中收到以下Json数据...我不知道如何遍历它我wana循环通过它并从中创建一个数组,我将在HTML DOM中使用我也是我的缓存如果数据已经可用,那么此更多Ajax请求的数据......
我怎么能这样做, 我试过用$ .each(数据,函数(键,值){----})来做。但问题是循环遍历数据我想为循环和内部我想要填充一些其他数组或对象......
这有意义吗,我们可以这样做吗?
{"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"}
答案 0 :(得分:1)
您的问题可以通过两个步骤解决:
1)将您的json数据转换为php String 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/>
。进一步格式化输出作为编码器的练习: - )
答案 1 :(得分: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”, “语言”: “帕皮阿门托”},{ “COUNTRYCODE”: “ABW”, “语言”: “西班牙语”},{ “COUNTRYCODE”: “AFG”, “语言”: “俾路支” },{ “COUNTRYCODE”: “AFG”, “语言”: “达日”},{ “COUNTRYCODE”: “AFG”, “语言”: “普什图语”},{ “COUNTRYCODE”: “AFG”, “语言” : “土库曼斯坦”},{ “COUNTRYCODE”: “AFG”, “语言”: “乌”},{ “COUNTRYCODE”: “前”, “语言”: “安博”},{ “COUNTRYCODE”: “AGO” “语言”: “绍奎”},{ “COUNTRYCODE”: “前”, “语言”: “金刚”},{ “COUNTRYCODE”: “前”, “语言”: “Luchazi”},{ “COUNTRYCODE” : “AGO”, “语言”: “Luimbe-nganguela”},{ “COUNTRYCODE”: “前”, “语言”: “Luvale”},{ “COUNTRYCODE”: “前”, “语言”: “Mbundu” },{ “COUNTRYCODE”: “前”, “语言”: “Nyaneka-nkhumbi”},{ “COUNTRYCODE”: “前”, “语言”: “Ovimbundu”},{ “COUNTRYCODE”: “AIA”,”语言 “:” 英语 “},{” COUNTRYCODE “:” ALB “ ”语言“: ”Albaniana“},{ ”COUNTRYCODE“: ”ALB“, ”语言“: ”希腊“},{ ”COUNTRYCODE“:” ALB “ ”语言“: ”马其顿“},{” 国家代码 “:” 和”, “语言”: “加泰罗尼亚”},{ “COUNTRYCODE”: “和”, “语言”: “法国”},{ “COUNTRYCODE”: “和”, “语言”: “葡萄牙” },{ “COUNTRYCODE”: “和”, “语言”: “西班牙”},{ “COUNTRYCODE”: “蚁族”, “语言”: “荷兰”},{ “COUNTRYCODE”: “蚁族”, “语言” : “英语”},{ “COUNTRYCODE”: “蚁族”, “语言”: “帕皮阿门托”},{ “COUNTRYCODE”: “有”, “语言”: “阿拉伯语”},{ “COUNTRYCODE”: “ARE” ,“Language”:“Hindi”},{“CountryCode”:“ARG”,“Language”:“Indian Languages”},{“CountryCode”:“ARG”,“Language”:“Italian”},{“CountryCode “:” ARG”, “语言”: “西班牙语”},{ “COUNTRYCODE”: “ARM”, “语言”: “亚美尼亚”},{ “COUNTRYCODE”: “ARM”, “语言”: “阿塞拜疆”} { “COUNTRYCODE”: “ASM”, “语言”: “英语”},{ “COUNTRYCODE”: “ASM”, “语言”: “萨摩亚”},{ “COUNTRYCODE”: “ASM”, “语言”: “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 “ ”语言“:” 热尔马N “},{” COUNTRYCODE “:” AUS “ ”语言“: ”希腊“},{ ”COUNTRYCODE“: ”AUS“, ”语言“: ”意大利“},{ ”COUNTRYCODE“: ”AUS“,”语言 “:” 塞尔维亚 - 克罗地亚语 “},{” COUNTRYCODE “:” AUS”, “语言”: “越南”},{ “COUNTRYCODE”: “AUT”, “语言”: “捷克”},{ “COUNTRYCODE” : “AUT”, “语言”: “德国”},{ “COUNTRYCODE”: “AUT”, “语言”: “匈牙利”},{ “COUNTRYCODE”: “AUT”, “语言”: “波兰”}, { “COUNTRYCODE”: “AUT”, “语言”: “罗马尼亚”},{ “COUNTRYCODE”: “AUT”, “语言”: “塞尔维亚克罗地亚语”},{ “COUNTRYCODE”: “AUT”, “语言” : “斯洛文尼亚”},{ “COUNTRYCODE”: “AUT”, “语言”: “土耳其”},{ “COUNTRYCODE”: “阿塞拜疆”, “语言”: “亚美尼亚”},{ “COUNTRYCODE”: “阿塞拜疆” “语言”: “阿塞拜疆”},{ “COUNTRYCODE”: “阿塞拜疆”, “语言”: “Lezgian”},{ “COUNTRYCODE”: “阿塞拜疆”, “语言”: “俄罗斯”},{ “COUNTRYCODE” “BDI”, “语言”: “法国”},{ “COUNTRYCODE”: “BDI”, “语言”: “基隆迪”},{ “COUNTRYCODE”: “BDI”, “语言”: “斯瓦希里语”}, { “COUNTRYCODE”: “BEL”, “语言”: “阿拉伯语”},{ “COUNTRYCODE”: “BEL”, “语言”: “荷兰”},{ “COUNTRYCODE”: “BEL”, “语言”:”法国 “},{” COUNTRYCODE “:” BEL “ ”语言“:” GER男人 “},{” COUNTRYCODE “:” BEL “ ”语言“: ”意大利“},{ ”COUNTRYCODE“: ”BEL“, ”语言“: ”土耳其“},{ ”COUNTRYCODE“: ”奔“,”语言 “:” ADJA “},{” COUNTRYCODE “:” BEN”, “语言”: “爱藏”}]');
警报(countries.length);