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?

JavaScript/Python:備忘録と勉強

Posted at

Python側でロードしたデータをJavaScriptで保持してHTMLで使用したかったため
部分的なものとなるがjsonへの変換方法を記録する

msg_jsonの値をHTML側で呼び出し、JavaScriptで処理を書くことで取り出すことが可能となる

# -*- coding: utf-8 -*-
import json  # jsonモジュールのインポート

class Main():
    def __init__(self):
        self.templates = [
            {
                'title': 'テストタイトル',
                'body': 'テスト本文',
                'signature': 'テスト署名'
            },
            {
                'title': 'テストタイトル1',
                'body': 'テスト本文2',
                'signature': 'テスト署名2'
            }
        ]

    def main(self):
        # JSON文字列を作成
        # 日本語対応のため ensure_ascii=False
        msg_json = json.dumps(self.templates, ensure_ascii=False)
        # 結果を返す
        return msg_json

# クラスのインスタンスを作成
main_instance = Main()
# メソッドを呼び出し、結果を表示
result = main_instance.main()
print(result)

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?