Monday, January 16, 2012

[Level 2] JSON module in Python.

#!/bin/env python
import json
import random

data = {}
for i in range(0,2):
    key = 'i%s' % i 
    data[key] = {}
    for j in range(0,3):
        subkey = 'j%s' % j 
        data[key][subkey] = {}
        for k in range(0,4):
            item = 'k%s' % k 
            value = int(random.random() * 100)
            data[key][subkey][item] = value
print json.dumps(data)
$ ./testPython.py 
{"i1": {"j0": {"k3": 93, "k2": 98, "k1": 0, "k0": 3}, "j1": {"k3": 53, "k2": 28, "k1": 99, "k0": 69}, "j2": {"k3": 3, "k2": 93, "k1": 74, "k0": 28}}, "i0": {"j0": {"k3": 67, "k2": 98, "k1": 15, "k0": 92}, "j1": {"k3": 70, "k2": 22, "k1": 63, "k0": 3}, "j2": {"k3": 88, "k2": 28, "k1": 30, "k0": 54}}}
Wish this helps.
regards,
Stanley Huang