【问题标题】:how read data JSON with request.body如何使用 request.body 读取数据 JSON
【发布时间】:2015-05-31 00:40:03
【问题描述】:

我从 jquery 收到 json:

dataJSON = urllib.unquote(request.body.decode("utf-8"))
print dataJSON
horarios=[{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0},{"horario":"6_20_23","estado":0}]

我尝试使用 for 读取数据:

for key in dataJSON:
    print key

【问题讨论】:

    标签: jquery python json django-views


    【解决方案1】:
    import json
    
    dataJSON = json.loads(request.body.decode("utf-8"))
    
    for key in dataJSON:
        print key
    

    我不确定你是否需要urllib.unquotedataJSON = json.loads(urllib.unquote(request.body.decode("utf-8"))),两者都试一下看看

    【讨论】:

    • 两者都给我以下错误:无法解码 JSON 对象
    • 编辑您的问题并添加您收到的 request.body 内容,可能存在一些字符串或空格字符
    【解决方案2】:

    解决了以下问题:

    dataJSON2 = []
    dataJSON = urllib.unquote(request.body.decode("utf-8"))
    dataJSON2 = dataJSON[9:]
    dataJSON2 = json.loads(dataJSON2)
    for key in dataJSON2:
        print key['horario']
    

    【讨论】:

      猜你喜欢
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多