1

JSON 文字列があり、tkinter GUI ウィンドウでラベルを使用して各セクション (ID、名前、リンクなど) を印刷しようとしています。

データ:

{"id":"123456789","name":"John Smith","first_name":"John","last_name":"Smith","link":"http:\/\/www.facebook.com\/john.smith","username":"john.smith","gender":"male","locale":"en_GB"}

コード:

URL = https://graph.facebook.com/ + user
info = urlopen(info).read()
json_format = infor.decode("utf-8")

私の問題は、json データの各セクションを変数に割り当てる方法です。tkinter ラベルに出力できますか?

前もって感謝します

編集

このコードを試しました:

jsonData = json.loads(json_format)
u_name = jsoninfo['username']

次のエラーメッセージが表示されました

TypeError: string indices must be integers
4

3 に答える 3

6

json標準モジュールを使用したい:

>>> import json
>>> data = '{"id":"123456789","name":"John Smith","first_name":"John","last_name":"Smith","link":"http:\/\/www.facebook.com\/john.smith","username":"john.smith","gender":"male","locale":"en_GB"}'
>>> d = json.loads(data)

これにより、使用する通常の辞書としてデータが提供されます。

>>> d
{u'username': u'john.smith', u'first_name': u'John', u'last_name': u'Smith', u'name': u'John Smith', u'locale': u'en_GB', u'gender': u'male', u'link': u'http://www.facebook.com/john.smith', u'id': u'123456789'}
>>> d['username']
u'john.smith'
于 2013-03-15T13:13:14.977 に答える
1
try:
    import simplejson as json
except ImportError: 
    import json

json_data = json.dumps(info)
# info here is json string or your variable json_format
于 2013-03-15T13:10:56.663 に答える
0

ライブラリをインポートする必要がありjsonます - これは標準ライブラリに含まれており、json をロードします。これにより、json 文字列が使用可能な Python 辞書に変換されます。

import json

py_dict= json.loads(json_string)
# work away
于 2013-03-15T13:14:03.280 に答える