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

2017-07-05 10:05:43 +08:00
 1002149271

前端 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 解析什么的,但是我还是不知道怎么把返回的这个数据赋值给前端的一个对象。。。 求那个大佬教教菜鸡,怎么把它正常输出像表格那样一行一行的。。。有代码就真的是感谢了

8063 次点击
所在节点    Python
12 条回复
ytmsdy
2017-07-05 10:10:37 +08:00
试试
str.dict()
不行就
json.loads(str )
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
2017-07-05 10:28:47 +08:00
@ptrees 我也是感觉我返回的好像不对。。。。看网上都是用的 render
1002149271
2017-07-05 10:30:38 +08:00
@ytmsdy 还是不知道前端怎么获取返回。。。
1002149271
2017-07-05 10:34:00 +08:00
@ptrees 我这个 available_train 好像是个 json 数据,不是 list 和 dict 也可以这样用吗
1002149271
2017-07-05 10:39:23 +08:00
@ptrees thinks so,我好像知道一点点,我这个 available_train 确实是个 list
okletswin
2017-07-05 10:42:44 +08:00
需求就很奇怪,点击按钮后页面发生变化,不是该服务端返回 html 的吗?

如果要返回 json, 要设置 Context 类型 application/json
kevanbin
2017-07-05 10:43:30 +08:00
json.loads()
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
2017-07-05 11:23:03 +08:00
return HttpResponse(json.dumps(...))
mkeith
2017-07-05 11:27:36 +08:00
你的 appkey 泄露啦
wind3110991
2017-07-05 19:58:39 +08:00
后台 json.loads(str)一下就解析了,
建议学框架前系统好好学习 python 的基础知识,难道现在的技术人员都是成果驱动学习的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/373133

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX