在javascript中反序列化JSON字典列表

时间:2013-10-11 09:08:22

标签: javascript jquery python json cherrypy

我正在使用CherrPy,我必须建立一个AJAX连接来检索有关表的信息。 python代码是:

    @cherrypy.expose
def projects(self):
    c = self.dbConnect()
    projects_list = list()
    projects_version_list = list()

    # Get every products
    c.execute('Select * from T_Projects')
    result = c.fetchall()
    for row in result:

        # Conversion ID to string
        id = self.b10to26(int(str(row).split(",")[0][1:]))
        if len(id)==1:
            id = "AA" + id
        elif len(id)==2:
            id = "A" + id

        # Parse db informations
        name = str(row).split("'")[1] 
        created = str(row).split(",")[2].split("'")[1]

        # Product in dict line
        dict = {"name" : name, "id" : id, "created" : created}

        # Get every product versions
        cmd = ('Select * from T_Projects_Versions where id_project="{}"').format(str(row).split(",")[0][1:])
        c.execute(cmd)
        versions = c.fetchall()
        for row_version in versions:

            # Get ID + Name
            id_version = self.b10to26(int(str(row_version).split(",")[1]))
            if len(id_version)==1:
                id_version = "A" + id_version
            name = str(row_version).split("'")[1]
            created = str(row_version).split(",")[4].split("'")[1]

            # Product version dict line
            dict_version = {"name" : name, "id" : id_version, "created" : created}

            #Put in a list
            projects_version_list.append (dict_version)
        projects_list.append({"dict" : dict, "projects_version_list" : projects_version_list})

    cherrypy.response.headers["Content-Type"] = "application/json"
    return json.dumps(projects_list)

Javascript / Jquery代码在这里:

for(var i = 0;i < data.length; ++i) //for enumerating array
                {
                     var obj = data[i];
                     for(var propName in obj) //for enumerating the properties of an object
                     {
                         var value = obj[propName];
                         $('#container').append("item : " + i + " : prop : " + propName + " : value : " + value);
                     }
                }

输出是:

  

item:0:prop:dict:value:[object Object] item:0:prop:projects_version_list:value:[object Object],[object Object],[object Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] ...

javascript部分无效。你有什么建议吗?

谢谢!

Servietsky

1 个答案:

答案 0 :(得分:0)

我几天前解决了这个问题,也许会引起别人的兴趣。

这是js:

$.get( "/products", function(data) {
        for(var i = 0;i < data.length; ++i) 
        {
            $('.products_body').append("<tr class='header'><td id='name' class='product'>" + data[i]["product"]['name'] + "</td><td class='product'> " + data[i]["product"]['id'] + "</td><td class='product'>" + data[i]["product"]['created'] +  "</td></tr>");
            for(var j = 0;j < data[i]["versions"].length; ++j)
            {
                var value = data[i]["versions"][j];
                $('.products_body').append("<tr class='versions'><td class='version'>" + value['name'] + " </td><td class='version'> " + value['id'] + "</td><td class='version'>" + value['created'] + "</td></tr>");
            }
        }
    })

这是蟒蛇:

@cherrypy.expose
def products(self):
    c = self.dbConnect()
    products = list()

    # Get every products
    c.execute('Select * from T_products')
    result = c.fetchall()
    for row in result:
        versions = list()
        # Conversion ID to string
        id = self.b10to26(int(str(row).split(",")[0][1:]))
        if len(id)==1:
            id = "AA" + id
        elif len(id)==2:
            id = "A" + id

        # Parse db informations
        name = str(row).split("'")[1] 
        created = str(row).split(",")[2].split("'")[1]

        # Product in dict line
        product = {"name" : name, "id" : id, "created" : created}

        # Get every product versions
        cmd = ('Select * from T_products_Versions where id_product="{}"').format(str(row).split(",")[0][1:])
        c.execute(cmd)
        cmd_versions = c.fetchall()
        for row_version in cmd_versions:

            # Get ID + Name
            id_version = self.b10to26(int(str(row_version).split(",")[1]))
            if len(id_version)==1:
                id_version = "A" + id_version
            name = str(row_version).split("'")[1]
            created = str(row_version).split(",")[4].split("'")[1]

            # Product version dict line
            version = {"name" : name, "id" : id_version, "created" : created}

            #Put in a list
            versions.append (version)
        products.append({"product" : product, "versions" : versions})

    cherrypy.response.headers["Content-Type"] = "application/json"
    return json.dumps(products)