0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#twitter #twitterapi (バージョン1.1)で取得したデータの文字化け対処( #Javascript )

Posted at

本内容を利用した場合の一切の責任を私は負いません。

バージョン

  • node.js
    node-v10.16.0-win-x64
  • twitterモジュール
    twitter-node-client@0.0.6
  • Visual Studio Code(以降、VSC)
    VSCode-win32-x64-1.38.1

レスポンスはUTF-8のようだが、何故か正常に認識しない。
(ファイルに出力してVSCで見た時に文字コードで表示される。)
対処は簡単で、JSONのパースと文字列化を再試行する。
レスポンスは文字列なので、それをパースし、また文字列化するということ。

具体的には下記。

    let tmp;
    fs.writeFileSync("result0.json", ret.detail[0]);
    tmp = JSON.parse(ret.detail[0]);
    let tmp2;
    tmp2 = JSON.stringify(tmp);
    fs.writeFileSync("result1.json", tmp2);

    twitterUrl = Client.baseUrl + "/search/tweets.json" + tmp.search_metadata.next_results;
    ret = await syncDoRequest(twitterUrl);
    tmp = JSON.parse(ret.detail[0]);
    tmp2 = JSON.stringify(tmp);
    fs.writeFileSync("result2.json", tmp2);

ret.detail[0]の中にレスポンスのJSON文字列が入っている。
result0.jsonと再試行したresult2.jsonは論理的には同じはずだが、実際に後者は文字化けが解消されている。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?