我在頁面的腳本標記內部有JSON數據。我試着像下面通過引入nokogiri:ruby JSON解析器錯誤
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數據中的所有斜線,然後解析它,或者我怎樣纔能有效地解析它?
感謝它的工作 – rubyist 2015-04-04 04:55:17