在JS中序列化/修复以下json / yaml

时间:2011-04-08 12:52:00

标签: javascript json parsing yaml

我需要谷歌地图建议
(不幸的是,官方地理编码API无法提供真正的建议,只有地理编码)

目前有两种版本可供选择:
http://maps.google.de/maps/suggest?q=test&cp=1&hl=de&gl=de&v=2&json=b(YAML)
http://maps.google.de/maps/suggest?q=test&cp=1&hl=de&gl=de&v=2&json=a(无效的JSON)

所以json.parse不起作用。

谁最了解如何处理这些数据?解析它?修复它解析它?

2 个答案:

答案 0 :(得分:2)

JSON.parse()不对任何网址返回的数据有效,但eval()同时适用于这两种数据。

var a = eval({suggestion:[{query:"Texas, USA",interpretation:{term:[{start:0,end:5,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:7,end:10,feature_type:""}]},operation:2,target_type:545},{query:"Toronto, Ontario, Kanada",interpretation:{term:[{start:0,end:7,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:9,end:16,feature_type:""},{start:18,end:24,feature_type:""}]},operation:2,target_type:37},{query:"Tennessee, USA",interpretation:{term:[{start:0,end:9,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:11,end:14,feature_type:""}]},operation:2,target_type:545},{query:"Turin, Italien",interpretation:{term:[{start:0,end:5,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:7,end:14,feature_type:""}]},operation:2,target_type:37},{query:"Tampa, Florida, USA",interpretation:{term:[{start:0,end:5,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:7,end:14,feature_type:""},{start:16,end:19,feature_type:""}]},operation:2,target_type:37},{query:"Toulouse, Frankreich",interpretation:{term:[{start:0,end:8,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:10,end:20,feature_type:""}]},operation:2,target_type:37},{query:"Tucson, Arizona, USA",interpretation:{term:[{start:0,end:6,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:8,end:15,feature_type:""},{start:17,end:20,feature_type:""}]},operation:2,target_type:37},{query:"Tokio, Japan",interpretation:{term:[{start:0,end:5,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:7,end:12,feature_type:""}]},operation:2,details:[{value:"日本東京都",interpretation:{term:[{start:0,end:2,feature_type:""},{start:2,end:5,feature_type:"",matched:1,target:1,term_start:0,term_end:4}]}}],target_type:8724},{query:"Teneriffa, Spanien",interpretation:{term:[{start:0,end:9,feature_type:"",matched:1,target:1,term_start:0,term_end:4},{start:11,end:18,feature_type:""}]},operation:2,target_type:2626},{query:"Thailand",interpretation:{term:[{start:0,end:8,feature_type:"",matched:1,target:1,term_start:0,term_end:4}]},operation:2,target_type:33}]});
var b = eval([,,,[["Texas, USA",[[[0,5,,,1,,,,1,0,4],[7,10]]],2,,,,,,,,,,,,,,545],["Toronto, Ontario, Kanada",[[[0,7,,,1,,,,1,0,4],[9,16],[18,24]]],2,,,,,,,,,,,,,,37],["Tennessee, USA",[[[0,9,,,1,,,,1,0,4],[11,14]]],2,,,,,,,,,,,,,,545],["Turin, Italien",[[[0,5,,,1,,,,1,0,4],[7,14]]],2,,,,,,,,,,,,,,37],["Tampa, Florida, USA",[[[0,5,,,1,,,,1,0,4],[7,14],[16,19]]],2,,,,,,,,,,,,,,37],["Toulouse, Frankreich",[[[0,8,,,1,,,,1,0,4],[10,20]]],2,,,,,,,,,,,,,,37],["Tucson, Arizona, USA",[[[0,6,,,1,,,,1,0,4],[8,15],[17,20]]],2,,,,,,,,,,,,,,37],["Tokio, Japan",[[[0,5,,,1,,,,1,0,4],[7,12]]],2,,,,,,,[["日本東京都",,,[[[0,2],[2,5,,,1,,,,1,0,4]]]]],,,,,,,8724],["Teneriffa, Spanien",[[[0,9,,,1,,,,1,0,4],[11,18]]],2,,,,,,,,,,,,,,2626],["Thailand",[[[0,8,,,1,,,,1,0,4]]],2,,,,,,,,,,,,,,33]]]);

答案 1 :(得分:2)

两者都是无效的JSON,但是有效的javascript。您可以使用eval来解析它们,尽管它会产生安全风险。即

eval("var y =" + text);