Node.jsでGoogleカスタム検索を呼び出してみます。 JSON.parse(dataFromGoogle)を使用してJSONを解析しようとすると、多数の要素(HTMLのタイトルとスニペット、HTMLのタイトルにはユニコードのエスケープシーケンスが含まれています)で不正なトークンエラーが発生します、私はスニペットで何が間違っているのか分かりません)。私はGoogleにHTMLのタイトルを返信させないようにすることができますが、本当にスニペットが必要です!無効なJSONを返すGoogleカスタム検索API?
これには適切な回避策がありますか、または手動で不正な文字を取り除くための追加の前処理を行うだけでよいですか?
* *編集:それは私は完全に誤りが私に言っていたものを誤解が判明して、Googleの
{ "kind": "customsearch#search", "url": { "type": "application/json", "template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&hr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&alt=json" }, "queries": { "nextPage": [ { "title": "Google Custom Search - small business", "totalResults": "42300", "searchTerms": "small business", "count": 10, "startIndex": 11, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "my_token" } ], "request": [ { "title": "Google Custom Search - small business", "totalResults": "42300", "searchTerms": "small business", "count": 10, "startIndex": 1, "inputEncoding": "utf8", "outputEncoding": "utf8", "safe": "off", "cx": "my_token" } ] }, "context": { "title": "IR undefined:60 "htmlTitle": "\u003cb\u003eSmall Business\u003c/b\u003e Health Care Tax Cre ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Unexpected token ILLEGAL at Object.parse (native) at IncomingMessage. (/Users/pvencill/workspace/irslab/lib/searchEngine.js:44:35) at IncomingMessage.emit (events.js:64:17) at HTTPParser.onBody (http.js:119:42) at CleartextStream.ondata (http.js:1213:22) at CleartextStream._push (tls.js:291:27) at SecurePair._cycle (tls.js:565:20) at EncryptedStream.write (tls.js:97:13) at Socket.ondata (stream.js:40:26) at Socket.emit (events.js:64:17)
どのような応答がありますか? – Chance
いくつかのフィールドにユニコードエスケープが含まれている点を除いて、適切なJSONです。私はこれらのフィールドを一時的な修正として除外しましたが、最終的にはそれらを持っていきたいと思います。私は明日私が持っていたものを掲示します、私は私の作業機械でそれをやっていました。 – Paul