V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
1002149271
V2EX  ›  Python

django 做的网页怎么解析后端返回的 json 数据。

  •  
  •   1002149271 · 2017-07-05 10:05:43 +08:00 · 8046 次点击
    这是一个创建于 2458 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前端 index.html 是这样写的:

                出发站点: <input type="text" name="a"> <br>
                目的站点: <input type="text" name="b"> <br>
                出发日期: <input type="text" name="c"> <br>
                <input type="submit" value="提交">
            </form>
    

    后端 view->train 函数里是这样写返回数据的:

        from_station=stations.get(a)
        to_station=stations.get(b)
        date = c
        options = ''
        url = 'http://op.juhe.cn/trainTickets/ticketsAvailable?dtype=&train_date={}&from_station={}&to_station={}&key=d655088e9b37ad7f9b938ac6c17d7233'.format(
            date, from_station, to_station
        )
        print(from_station, to_station, date, options, url)
        """获取参数"""
        r = requests.get(url, verify=False)
        available_trains = r.json()['result']['list']
        return HttpResponse(available_trains)
    

    然后输入数据点击提交后到新页面输出一堆乱七八糟的数据:

    {'gjrws_price': 0, 'train_code': 'K829', 'yz_num': '0', 'ywz_price': 462.5, 'to_station_name': '广州', 'rz_num': '--', 'start_time': '10:10', 'ydz_price': 0, 'run_time': '43:00', 'run_time_minute': '2580', 'from_station_name': '成都', 'sale_date_time': '0900', 'rw_num': '0', 'rw_price': 704.5, 'wz_price': 263.5, 'train_start_date': '20170705', 'arrive_days': '2', 'dw_num': '--', 'dwx_price': 0, 'swz_num': '--', 'qtxb_num': '--', 'edz_price': 0, 'can_buy_now': 'Y', 'end_station_name': '广州', 'ydz_num': '--', 'yw_num': '0', 'tdz_num': '--', 'access_byidcard': '0', 'rz_price': 0, 'dw_price': 0, 'ywx_price': 477.5, 'swz_price': 0, 'wz_num': '104', 'tdz_price': 0, 'edz_num': '--', 'yz_price': 263.5, 'rwx_price': 735.5, 'gjrw_num': '--', 'yw_price': 446.5, 'start_station_name': '成都', 'from_station_code': 'CDW', 'gjrw_price': 0, 'train_no': '760000K8290E', 'to_station_code': 'GZQ', 'qtxb_price': 0, 'train_type': 'K', 'arrive_time': '05:10'}{'gjrws_price': 0, 'train_code': 'K1223', 'yz_num': '0', 'ywz_price': 462.5, 'to_station_name': '广州东', 'rz_num': '--', 'start_time': '13:00', 'ydz_price': 0, 'run_time': '44:19', 'run_time_minute': '2659', 'from_station_name': '成都', 'sale_date_time': '0900', 'rw_num': '0', 'rw_price': 704.5, 'wz_price': 263.5, 'train_start_date': '20170705', 'arrive_days': '2', 'dw_num': '--', 'dwx_price': 0, 'swz_num': '--', 'qtxb_num': '--', 'edz_price': 0, 'can_buy_now': 'Y', 'end_station_name': '广州东', 'ydz_num': '--', 'yw_num': '0', 'tdz_num': '--', 'access_byidcard': '0', 'rz_price': 0, 'dw_price': 0, 'ywx_price': 477.5, 'swz_price': 0, 'wz_num': '112', 'tdz_price': 0, 'edz_num': '--', 'yz_price': 263.5, 'rwx_price': 735.5, 'gjrw_num': '--', 'yw_price': 446.5, 'start_station_name': '成都', 'from_station_code': 'CDW', 'gjrw_price': 0, 'train_no': '76000K12230F', 'to_station_code': 'GGQ', 'qtxb_price': 0, 'train_type': 'K', 'arrive_time': '09:19'}
    

    这个数据是以{}这个分开为一行,中间没有逗号,好像又不是标准的 json 数据。。。 百度了很多了。好像需要什么 eval 解析什么的,但是我还是不知道怎么把返回的这个数据赋值给前端的一个对象。。。 求那个大佬教教菜鸡,怎么把它正常输出像表格那样一行一行的。。。有代码就真的是感谢了

    12 条回复    2017-07-05 19:58:39 +08:00
    ytmsdy
        1
    ytmsdy  
       2017-07-05 10:10:37 +08:00
    试试
    str.dict()
    不行就
    json.loads(str )
    ptrees
        2
    ptrees  
       2017-07-05 10:21:32 +08:00
    应该是用模板
    <ul>
    {% for key, value in available_trains.items %}
    <li>{{ key }}: {{ value }}</li>
    {% endfor %}
    </ul>
    返回的方式好像不对,应该用 render 渲染一个页面来显示吧
    贴个教程 http://code.ziqiangxuetang.com/django/django-template2.html
    我也还在学习,讲得很不错
    1002149271
        3
    1002149271  
    OP
       2017-07-05 10:28:47 +08:00
    @ptrees 我也是感觉我返回的好像不对。。。。看网上都是用的 render
    1002149271
        4
    1002149271  
    OP
       2017-07-05 10:30:38 +08:00
    @ytmsdy 还是不知道前端怎么获取返回。。。
    1002149271
        5
    1002149271  
    OP
       2017-07-05 10:34:00 +08:00
    @ptrees 我这个 available_train 好像是个 json 数据,不是 list 和 dict 也可以这样用吗
    1002149271
        6
    1002149271  
    OP
       2017-07-05 10:39:23 +08:00
    @ptrees thinks so,我好像知道一点点,我这个 available_train 确实是个 list
    okletswin
        7
    okletswin  
       2017-07-05 10:42:44 +08:00
    需求就很奇怪,点击按钮后页面发生变化,不是该服务端返回 html 的吗?

    如果要返回 json, 要设置 Context 类型 application/json
    kevanbin
        8
    kevanbin  
       2017-07-05 10:43:30 +08:00
    json.loads()
    vicalloy
        9
    vicalloy  
       2017-07-05 10:51:39 +08:00
    https://docs.djangoproject.com/en/1.11/ref/request-response/#jsonresponse-objects

    JsonResponse objects
    class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs)[source]¶
    shyling
        10
    shyling  
       2017-07-05 11:23:03 +08:00
    return HttpResponse(json.dumps(...))
    mkeith
        11
    mkeith  
       2017-07-05 11:27:36 +08:00
    你的 appkey 泄露啦
    wind3110991
        12
    wind3110991  
       2017-07-05 19:58:39 +08:00
    后台 json.loads(str)一下就解析了,
    建议学框架前系统好好学习 python 的基础知识,难道现在的技术人员都是成果驱动学习的?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2783 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:24 · PVG 08:24 · LAX 17:24 · JFK 20:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.