{“{\”3 \“:\”迪拜\“,”2“:”阿治曼“,”1“:”阿布扎比“,”7“: “Umm al-Quwain”,“6”:“沙迦”,“5”:“Ras al-Khaimah”,“4”:“Fujairah \”}“:” {\“3 \”:\“迪拜”,“2”:“阿治曼”,“1”:“阿布扎比”,“7”:“Umm al-Quwain \“,\”6 \“:\”沙迦“,”5“:”Ras al-Khaimah“,”4“:\”Fujairah \“}”,“{\”17 \ “:”泰语“,”18“:”韩语“,”15“:”繁体中文“,”16“:”简体中文“,”14“ “:”英语(亚洲)\“}”:“{\”17 \“:\”泰语\“,\”18 \“:\”韩语“,”15“:”繁体中文“ “,\”16 \“:\”简体中文\“,\”14 \“:\”英文(亚洲)\“}”}
答案 0 :(得分:0)
目前尚不清楚你究竟希望解析的结果如何,但这里有:
var raw = '{"{\"3\":\"Dubai\",\"2\":\"Ajman\",\"1\":\"Abu Dhabi\",\"7\":\"Umm al-Quwain\",\"6\":\"Sharjah\",\"5\":\"Ras al-Khaimah\",\"4\":\"Fujairah\"}":"{\"3\":\"Dubai\",\"2\":\"Ajman\",\"1\":\"Abu Dhabi\",\"7\":\"Umm al-Quwain\",\"6\":\"Sharjah\",\"5\":\"Ras al-Khaimah\",\"4\":\"Fujairah\"}","{\"17\":\"Thai\",\"18\":\"Korean\",\"15\":\"Traditional Chinese\",\"16\":\"Simplified Chinese\",\"14\":\"English (Asia)\"}":"{\"17\":\"Thai\",\"18\":\"Korean\",\"15\":\"Traditional Chinese\",\"16\":\"Simplified Chinese\",\"14\":\"English (Asia)\"}"}';
var re = /\"(\d+)\":\"(.*?)\"/g,
matches = [],
match;
while(match = re.exec(raw)) {
matches.push(match);
}
var parsed = matches.reduce(function(result, cur) {
result[cur[1]] = cur[2];
}, {});
console.log(parsed);
> Object {1: "Abu Dhabi", 2: "Ajman", 3: "Dubai", 4: "Fujairah", 5: "Ras al-Khaimah", 6: "Sharjah", 7: "Umm al-Quwain", 14: "English (Asia)", 15: "Traditional Chinese", 16: "Simplified Chinese", 17: "Thai", 18: "Korean"}
或者,如果您希望将这些解析为数组,请将reducer更改为:
var parsed = matches.reduce(function(result, cur) {
result[cur[1] - 1] = cur[2];
return result;
}, []);
console.log(parsed);
> ["Abu Dhabi", "Ajman", "Dubai", "Fujairah", "Ras al-Khaimah", "Sharjah", "Umm al-Quwain", undefined × 6, "English (Asia)", "Traditional Chinese", "Simplified Chinese", "Thai", "Korean"]