2012-05-18 3 views
0

Я посылаю параметр пост какИзвлечь два словаря из одного словаря

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'] 

, но я получаю ошибки строковые индексы должны быть целыми числами, а не ул Как создать две переменных (словарь) elemorder и контейнер?

+0

Почему не использовать 11.1. pickle - Сериализация объектов Python - Документация Python v2.7.3 -> http://docs.python.org/library/pickle.html для сериализации данных? – Efazati

ответ

3

От запроса POST вы получаете str не dict. Попробуйте использовать JSon LIB как

import json 
css = json.loads(request.POST['q']) 
+0

Я думаю, что вы имеете в виду 'json.loads (request.POST ['q'])' – DrTyrsa

+0

Да, конечно)), но я смутил, какая тема стартерная структура получает запросы как строки, в пирамиде, например, она появилась как UnocodeMultiDict, например)) – Denis

2

Вам необходимо сериализовать данные, передать его в Django приложение, а затем десериализации.

JSON-сериализация выглядит хорошо для вашей задачи.

Смежные вопросы