我们有一个问题要解析js到列表。这类似于json解析,但在我的问题中只需要放置"markers"
列表中的数据。
示例,我们有来自http://localhost:4000/static/libary/jogja.js
的数据var jogjaAddress = {
"cityname": "Jogja",
"lat" : "-7.8722732",
"lon" : "110.1440828",
"markers": [
{
"name": "Keraton Yogyakarta",
"lat": "-7.8052845",
"lon": "110.3642031",
"image":"http://wisatanesia.co/wp-content/uploads/2015/12/keraton-yogya.jpg"
},
{
"name": "Malioboro",
"lat": "-7.793997",
"lon": "110.3634648",
"image": "http://anekatempatwisata.com/wp-content/uploads/2014/11/Malioboro.jpg"
},
{
"name": "Masjid Gedhe Kauman",
"lat": "-7.803951",
"lon": "110.3599771",
"image": "http://2.bp.blogspot.com/-E0lKq6W1M_g/TloYV5CmAJI/AAAAAAAADao/JefvauzcAv8/s1600/L1290134.JPG"
},
{
"name": "Taman Sari",
"lat": "-7.8099632",
"lon": "110.3567511",
"image": "http://www.maestrobali.com/en/wp-content/uploads/2015/04/Taman-Sari-Water-Castle1.jpg"
}
]
}
然后,输出应为:
myList = [
{
"name": "Keraton Yogyakarta",
"lat": "-7.8052845",
"lon": "110.3642031",
"image":"http://wisatanesia.co/wp-content/uploads/2015/12/keraton-yogya.jpg"
},
{
"name": "Malioboro",
"lat": "-7.793997",
"lon": "110.3634648",
"image": "http://anekatempatwisata.com/wp-content/uploads/2014/11/Malioboro.jpg"
},
{
"name": "Masjid Gedhe Kauman",
"lat": "-7.803951",
"lon": "110.3599771",
"image": "http://2.bp.blogspot.com/-E0lKq6W1M_g/TloYV5CmAJI/AAAAAAAADao/JefvauzcAv8/s1600/L1290134.JPG"
},
{
"name": "Taman Sari",
"lat": "-7.8099632",
"lon": "110.3567511",
"image": "http://www.maestrobali.com/en/wp-content/uploads/2015/04/Taman-Sari-Water-Castle1.jpg"
}
]
>>> import requests
>>> resp = requests.get('http://localhost:4000/static/libary/jogja.js')
>>> resp.text #or resp.content, it will return string
我可以将"markers"
的数据带到列表吗?
之前非常感谢。
答案 0 :(得分:0)
你所拥有的是几乎 json。它应该足以在第一个=
上拆分,然后用标准库json模块解析:
json_data = data.split('=', 1)[1]
myList = json.loads(json_data)['markers']
这是我终端的演示:
>>> import json
>>> s = """var jogjaAddress = {
... "cityname": "Jogja",
... "lat" : "-7.8722732",
... "lon" : "110.1440828",
...
... "markers": [
... {
... "name": "Keraton Yogyakarta",
... "lat": "-7.8052845",
... "lon": "110.3642031",
... "image":"http://wisatanesia.co/wp-content/uploads/2015/12/keraton-yogya.jpg"
... },
... {
... "name": "Malioboro",
... "lat": "-7.793997",
... "lon": "110.3634648",
... "image": "http://anekatempatwisata.com/wp-content/uploads/2014/11/Malioboro.jpg"
... },
... {
... "name": "Masjid Gedhe Kauman",
... "lat": "-7.803951",
... "lon": "110.3599771",
... "image": "http://2.bp.blogspot.com/-E0lKq6W1M_g/TloYV5CmAJI/AAAAAAAADao/JefvauzcAv8/s1600/L1290134.JPG"
... },
... {
... "name": "Taman Sari",
... "lat": "-7.8099632",
... "lon": "110.3567511",
... "image": "http://www.maestrobali.com/en/wp-content/uploads/2015/04/Taman-Sari-Water-Castle1.jpg"
... }
... ]
... }"""
...
>>> json_data = s.split('=', 1)[1]
>>> myList = json.loads(json_data)['markers']
>>> myList
[{u'lat': u'-7.8052845', u'image': u'http://wisatanesia.co/wp-content/uploads/2015/12/keraton-yogya.jpg', u'lon': u'110.3642031', u'name': u'Keraton Yogyakarta'}, {u'lat': u'-7.793997', u'image': u'http://anekatempatwisata.com/wp-content/uploads/2014/11/Malioboro.jpg', u'lon': u'110.3634648', u'name': u'Malioboro'}, {u'lat': u'-7.803951', u'image': u'http://2.bp.blogspot.com/-E0lKq6W1M_g/TloYV5CmAJI/AAAAAAAADao/JefvauzcAv8/s1600/L1290134.JPG', u'lon': u'110.3599771', u'name': u'Masjid Gedhe Kauman'}, {u'lat': u'-7.8099632', u'image': u'http://www.maestrobali.com/en/wp-content/uploads/2015/04/Taman-Sari-Water-Castle1.jpg', u'lon': u'110.3567511', u'name': u'Taman Sari'}]
答案 1 :(得分:0)
好的,所以我假设这是你正在解析的JSON。如果您在评论中添加了更多信息,我可以提供更多帮助
import json
resp = {your JSON String}
json_dict = json.loads(resp)
print(json_dict[markers])
这似乎是对Python附带的JSON模块的一种非常直接的使用,您是否对此进行过任何研究?查看here了解更多信息。
答案 2 :(得分:0)
这已经被问了太多次,你的变量jogjaAddress是一个字典,所以你应该这样对待它。考虑阅读有关数据结构的pythons文档:https://docs.python.org/3.4/tutorial/datastructures.html
最好将字典视为一组无序的键:值 对,要求密钥是唯一的(在一个内 字典)。一对大括号创建一个空字典:{}。配售 以逗号分隔的大括号内的键:值对列表添加 初始键:字典的值对;这也是方式 字典写在输出上。
并通过" key"检索元素根据你的代码是"标记"
换句话说,:
jogjaAddress ["markers"]
等于您要查找的输出。