使用ruby的JSON解析器错误

时间:2015-04-03 06:48:15

标签: javascript ruby json

我在页面的脚本标记内有JSON数据。我尝试下面使用Nokogiri:

json = doc.to_s[/var triforceManifestFeed = (.*)\r/, 1]

json包含以下格式的json数据:

"{\"manifest\":{\"css\":null,\"pageClass\":\"ent_p038_spike\",\"id\":\"56d116b5-e309-44ba-b5db-4e3ec78eadbc\",\"CMSTitle\":\"ENT_P038_SPIKE - Series Episode Guide\",\"parameters\":[],\"reporting\":{\"parentId\":\"5597d8e0-5eba-4eed-a9ea-a79f2d02002f\"},\"title\":\"Cops - Episode Guide | SPIKE.com\",\"type\":\"ENT_P038\",\"zones\":{\"t1_lc_promo1\":{\"feed\":\"http:\\/\\/www.spike.com\\/feeds\\/ent_m113_spike\\/2.0\\/1444d771-4d00-4a3e-973e-41a9847c69f2\\/5597d8e0-5eba-4eed-a9ea-a79f2d02002f\",\"feedData\":null,\"libraries\":[],\"version\":\"V3\",\"moduleName\":\"ENT_M113_Spike\",\"method\":\"triforce_v1\",\"priority\":0,\"driver\":\"ent_m113_spike_v2\",\"scriptHash\":\"ab1408769329d49\",\"zone\":\"t1_lc_promo1\"},\"t4_lc_promo1\":{\"feed\":\"http:\\/\\/www.spike.com\\/feeds\\/ent_m112_spike\\/2.0.1\\/62a1e116-2b7d-4160-ac2e-505f57572f0d\\/5597d8e0-5eba-4eed-a9ea-a79f2d02002f\",\"feedData\":null,\"libraries\":[],\"version\":\"V3\",\"moduleName\":\"ENT_M112_Spike\",\"method\":\"triforce_v1\",\"priority\":\"200\",\"driver\":\"ent_m112_spike_v2_0_1\",\"scriptHash\":\"8d876061f481e59\",\"zone\":\"t4_lc_promo1\"},\"ads-reporting\":{\"feed\":\"http:\\/\\/www.spike.com\\/modules\\/reporting\\/13ba010f-8f4e-4038-b8f8-b585175e92ca\",\"feedData\":null,\"libraries\":[],\"version\":\"V3\",\"moduleName\":\"ENT_SPIKE_Reporting\",\"method\":\"triforce_v1\",\"priority\":\"50\",\"driver\":\"reporting_v1\",\"scriptHash\":\"0b05d1e51beb47c\",\"zone\":\"ads-reporting\"},\"header\":{\"feed\":\"http:\\/\\/www.spike.com\\/modules\\/ent_m070_spike\\/4.2.0\\/57958c1a-ce7d-44a7-b17f-6d2b500b8275\\/5597d8e0-5eba-4eed-a9ea-a79f2d02002f\",\"feedData\":null,\"libraries\":[],\"version\":\"V3\",\"moduleName\":\"ENT_M070_Spike\",\"method\":\"dummy\",\"priority\":\"-100\",\"driver\":\"ent_m070_spike_v4_2_0\",\"scriptHash\":\"624c46a0e9ee2be\",\"zone\":\"header\"},\"footer\":{\"feed\":\"http:\\/\\/www.spike.com\\/modules\\/ent_m006_spike\\/1.0\\/78f6d8e3-6937-4f46-982c-9b900ba80709\",\"feedData\":null,\"libraries\":[],\"version\":\"V3\",\"moduleName\":\"ENT_M006_Spike\",\"method\":\"triforce_v1\",\"priority\":\"-100\",\"driver\":\"ent_m006_spike_v2\",\"scriptHash\":\"5c3648877102c5d\",\"zone\":\"footer\"}},\"showCSS\":null,\"supplemental\":{\"colors\":{\"contentType\":\"ColorScheme\",\"_arcv2FeedCheckPassed\":true,\"_arcv2ContentType\":\"Entertainment:ColorScheme\",\"shortId\":\"qditxa\",\"urlKey\":\"f9dfcbc0-1e66-4a60-92ea-8285c120bb0c\",\"namespace\":\"spike-site\",\"itemId\":\"f9dfcbc0-1e66-4a60-92ea-8285c120bb0c\",\"title\":\"Teal\",\"colors\":{\"primary\":\"#57B993\",\"secondary\":\"#D1D3D1\"},\"dependenciesForMakingBuilder\":null,\"_missingItems\":null,\"builderChainSuccessful\":true,\"depth\":0}}}};"

当我尝试解析(JSON.parse(json))时,它将json解析器错误显示为

JSON::ParserError: 757: unexpected token at ';'

我认为这是因为json数据中的斜杠。我是否需要删除json数据中的所有斜杠然后解析它,或者我如何有效地解析它?

2 个答案:

答案 0 :(得分:2)

分号可能是JavaScript的一部分,不应该作为JSON的一部分进行解析。这样做:

json = doc.to_s[/var triforceManifestFeed = (.*);\r/, 1]

答案 1 :(得分:1)

JSON字符串没有尾随分号;。如果你查看你的JSON字符串,它以;结束,因此错误:

  

JSON :: ParserError:757:';'

处的意外标记

从字符串末尾删除分号并重试。它应该工作正常。这里:

json.sub!(/;$/, '') # removing trailing ;
JSON.parse(json)
# => {"manifest"=>{"css"=>nil, "pageClass"=>"ent_p038_spike", "id"=>"56d116b5-e309-44ba-b5db-4e3ec78eadbc", "CMSTitle"=>"ENT_P038_SPIKE - Series Episode Guide", "parameters"=>[], "reporting"=>{"parentId"=>"5597d8e0-5eba-4eed-a9ea-a79f2d02002f"}, "title"=>"Cops - Episode Guide | SPIKE.com", "type"=>"ENT_P038", "zones"=>{"t1_lc_promo1"=>{"feed"=>"http://www.spike.com/feeds/ent_m113_spike/2.0/1444d771-4d00-4a3e-973e-41a9847c69f2/5597d8e0-5eba-4eed-a9ea-a79f2d02002f", "feedData"=>nil, "libraries"=>[], "version"=>"V3", "moduleName"=>"ENT_M113_Spike", "method"=>"triforce_v1", "priority"=>0, "driver"=>"ent_m113_spike_v2", "scriptHash"=>"ab1408769329d49", "zone"=>"t1_lc_promo1"}, "t4_lc_promo1"=>{"feed"=>"http://www.spike.com/feeds/ent_m112_spike/2.0.1/62a1e116-2b7d-4160-ac2e-505f57572f0d/5597d8e0-5eba-4eed-a9ea-a79f2d02002f", "feedData"=>nil, "libraries"=>[], "version"=>"V3", "moduleName"=>"ENT_M112_Spike", "method"=>"triforce_v1", "priority"=>"200", "driver"=>"ent_m112_spike_v2_0_1", "scriptHash"=>"8d876061f481e59", "zone"=>"t4_lc_promo1"}, "ads-reporting"=>{"feed"=>"http://www.spike.com/modules/reporting/13ba010f-8f4e-4038-b8f8-b585175e92ca", "feedData"=>nil, "libraries"=>[], "version"=>"V3", "moduleName"=>"ENT_SPIKE_Reporting", "method"=>"triforce_v1", "priority"=>"50", "driver"=>"reporting_v1", "scriptHash"=>"0b05d1e51beb47c", "zone"=>"ads-reporting"}, "header"=>{"feed"=>"http://www.spike.com/modules/ent_m070_spike/4.2.0/57958c1a-ce7d-44a7-b17f-6d2b500b8275/5597d8e0-5eba-4eed-a9ea-a79f2d02002f", "feedData"=>nil, "libraries"=>[], "version"=>"V3", "moduleName"=>"ENT_M070_Spike", "method"=>"dummy", "priority"=>"-100", "driver"=>"ent_m070_spike_v4_2_0", "scriptHash"=>"624c46a0e9ee2be", "zone"=>"header"}, "footer"=>{"feed"=>"http://www.spike.com/modules/ent_m006_spike/1.0/78f6d8e3-6937-4f46-982c-9b900ba80709", "feedData"=>nil, "libraries"=>[], "version"=>"V3", "moduleName"=>"ENT_M006_Spike", "method"=>"triforce_v1", "priority"=>"-100", "driver"=>"ent_m006_spike_v2", "scriptHash"=>"5c3648877102c5d", "zone"=>"footer"}}, "showCSS"=>nil, "supplemental"=>{"colors"=>{"contentType"=>"ColorScheme", "_arcv2FeedCheckPassed"=>true, "_arcv2ContentType"=>"Entertainment:ColorScheme", "shortId"=>"qditxa", "urlKey"=>"f9dfcbc0-1e66-4a60-92ea-8285c120bb0c", "namespace"=>"spike-site", "itemId"=>"f9dfcbc0-1e66-4a60-92ea-8285c120bb0c", "title"=>"Teal", "colors"=>{"primary"=>"#57B993", "secondary"=>"#D1D3D1"}, "dependenciesForMakingBuilder"=>nil, "_missingItems"=>nil, "builderChainSuccessful"=>true, "depth"=>0}}}}