0

我发送 post 参数,如

css=request.POST['q']
css=css.encode('utf-8')

和 css 有类似的内容

{
    "container": {
        "border-color": "white",
        "border-width": "0px",
        "height": "419px",
        "width": "200px",
        "border-style": "solid",
        "position": "relative",
        "background-color": "white"
    },
    "elemorder": {
        "productcode": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "40px",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center"
        },
        "name": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "40px",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center"
        },
        "url": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "40px",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center"
        },
        "price": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "40px",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center"
        },
        "id": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "40px",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center"
        },
        "imgurl": {
            "font-size": "14px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "227",
            "width": 200,
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "hidden",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center",
            "left": 0,
            "top": 80
        },
        "desc": {
            "font-size": "13px",
            "line-height": "18px",
            "color": "blue",
            "border-width": "0px",
            "letter-spacing": "0px",
            "text-decoration": "none",
            "height": "69",
            "width": "200px",
            "border-color": "white",
            "font-weight": "normal",
            "font-style": "normal",
            "position": "absolute",
            "overflow": "auto",
            "font-family": "Arial",
            "border-style": "solid",
            "display": "block",
            "background-color": "#3385D6",
            "text-align": "center",
            "left": 0,
            "top": 307
        }
    }
}

我把它放在css变量中。我想把字典像

elemorder=css['elemorder']
container=css['container']

但我得到错误字符串索引必须是整数,而不是 str如何创建两个变量(字典)elemorder 和容器?

4

2 回答 2

3

从 POST 请求中,您得到 str 而不是 dict。尝试使用 json lib 之类的

import json
css = json.loads(request.POST['q'])
于 2012-05-18T08:18:05.760 回答
2

您需要序列化数据,将其传递给 Django 应用程序,然后反序列化。

JSON 序列化看起来很适合您的任务。

于 2012-05-18T08:18:20.147 回答