ssk’s posterous

ssk’s posterous

Oct 29 / 1:04am

pythonとperlとphpとas3とjavascriptでJSON « taichino.com

by ssk

python
2.6系だとJSONが標準で組み込まれてるらしいんですが、手元は2.5系なのでsimplejsonを使っています。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, codecs
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
 
import simplejson
 
data = { 'items':[
  {'name':'iPhone',  'price':50000},
  {'name':'macbook', 'price':100000},
  {'name':"マクド",  'price':100},
]}
text = simplejson.dumps(data)  # encode
copy = simplejson.loads(text)  # decode
 
print "data = " + str(data)
print "text = " + text
print "copy = " + str(copy)
for item in copy['items']:
  print item["name"]

0 comments

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter