hor*_*uzz 3 rest parsing json flask flask-sqlalchemy
我有一个 REST API 端点,我需要在其中解析以下格式的传入嵌套 JSON:
site: {
id: '37251',
site_name: 'TestSite',
address: {
'address': '1234 Blaisdell Ave',
'city': 'Minneapolis',
'state': 'MN',
'zip': '55456',
'neighborhood': 'Kingfield',
'county': 'Hennepin',
},
geolocation: {
latitude : '41.6544',
longitude : '73.3322',
accuracy: '45'
}
}
Run Code Online (Sandbox Code Playgroud)
进入以下 SQLAlchemy 类:
地点:
class Site(db.Model):
__tablename__ = 'site'
id = Column(Integer, primary_key=True, autoincrement=True)
site_name = Column(String(80))# does site have a formal name
address_id = Column(Integer, ForeignKey('address.id'))
address = relationship("Address", backref=backref("site", uselist=False))
geoposition_id = Column(Integer, ForeignKey('geoposition.id'))
geoposition = relationship("Geoposition", backref=backref("site", uselist=False))
evaluations = relationship("Evaluation", backref="site")
site_maintainers = relationship("SiteMaintainer", backref="site")
Run Code Online (Sandbox Code Playgroud)
地址(一个站点有一个地址):
class Address(db.Model):
__tablename__ = 'address'
id = Column(Integer, primary_key=True, autoincrement=True)
address = Column(String(80))
city = Column(String(80))
state = Column(String(2))
zip = Column(String(5))
neighborhood = Column(String(80))
county = Column(String(80))
Run Code Online (Sandbox Code Playgroud)
和地理位置(一个站点有一个地理位置):
class Geoposition(db.Model):
__tablename__ = 'geoposition'
id = Column(Integer, primary_key=True, autoincrement=True)
site_id = Column(Integer)
latitude = Column(Float(20))
longitude = Column(Float(20))
accuracy = Column(Float(20))
timestamp = Column(DateTime)
Run Code Online (Sandbox Code Playgroud)
将 SQLAlchemey 数据导入 JSON 很容易,但我需要从我的请求中解析 JSON,以便我可以附加/更新通过 POST 发送到 RESTful API 的数据。我知道如何处理非嵌套 JSON,但我将首先承认我在处理属于关系结构中多个表的记录的嵌套 JSON 方面一无所知。
我试过在没有任何运气的情况下为此寻找高低。最近的我能找到的是这里的“嵌套验证与烧瓶宁静RequestParser”,但这不是点击了什么,我需要根据我的嵌套结构做。
凉爽的!看起来 Flask 通过请求处理程序处理这个:
使用这个 JSON:
site: {
"id": "37251",
"site_name": "TestSite",
"address": {
"address": "1234 Blaisdell Ave",
"city": "Minneapolis",
"state": "MN",
"zip": "55456",
"neighborhood": "Kingfield",
"county": "Hennepin"
},
geolocation: {
latitude : "41.6544",
longitude : "73.3322",
accuracy: "45"
}
}
Run Code Online (Sandbox Code Playgroud)
发送到此端点:
@app.route('/api/resource', methods=['GET','POST','OPTIONS'])
@cross_origin() # allow all origins all methods
@auth.login_required
def get_resource():
# Get the parsed contents of the form data
json = request.json
print(json)
# Render template
return jsonify(json)
Run Code Online (Sandbox Code Playgroud)
我得到以下对象:
{u'site': {u'geolocation': {u'latitude': u'41.6544', u'longitude': u'73.3322', u'accuracy': u'45'}, u'site_name': u'TestSite', u'id': u'37251', u'address': {u'city': u'Minneapolis', u'neighborhood': u'Kingfield', u'zip': u'55456', u'county': u'Hennepin', u'state': u'MN', u'address': u'1234 Blaisdell Ave'}}}
Run Code Online (Sandbox Code Playgroud)
更新:
使用此代码在我的端点中进行测试,我能够很好地访问我的所有字典项:
# print entire object
print json['site']
# define dictionary item for entire object
site = json['site']
print site["site_name"]
# print address object
print site['address']
# define address dictionary object
address = json['site']['address']
print address["address"]
# define geolocation dictionary object
geolocation = json['site']['geolocation']
print geolocation["accuracy"]
Run Code Online (Sandbox Code Playgroud)
现在回想起来,这似乎是微不足道的。我希望这对未来的人有所帮助。
| 归档时间: |
|
| 查看次数: |
8728 次 |
| 最近记录: |