继续获取类型错误python

时间:2015-10-30 12:48:06

标签: python string dictionary integer indices

我正在使用jqGrid进行搜索查询。我的参数如下:

function:GL
_search:true
nd:1446209493437
rows:50
page:1
sidx:datetime
sord:asc
filters:{"groupOp":"AND","rules":[{"field":"username","op":"eq","data":"user"},{"field":"description","op":"nc","data":"utility"}]}
searchField:
searchString:
searchOper:

当我使用这些值在本地运行脚本时,它工作正常,但是当我使用网页和cgi:

C:\wamp\www\ISaidItBest\assets\cgi-bin\app\Log.py in getAllLogs(self=<app.Log.Log object>)
     91                 elif filters:   # filter options
     92                     buildwhere = ""
=>   93                     rules = filters['rules']
     94                     for idx in range(len(rules)):
     95                         field = rules[idx]['field']
rules undefined, filters = '{"groupOp":"AND","rules":[{"field":"username","o...ield":"description","op":"nc","data":"utility"}]}'
TypeError: string indices must be integers 
      args = ('string indices must be integers',) 
      with_traceback = <built-in method with_traceback of TypeError object>

1 个答案:

答案 0 :(得分:0)

您需要将filtersstr转换为dict。阅读:https://docs.python.org/2/library/json.html#json.loads

尝试:

....
buildwhere = ""
if isinstance(filters, str):
    filters = json.loads(filters)
....