我有一个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'
}
}
进入以下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")
地址(网站有一个地址):
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))
和地理位置(网站有一个地理位置):
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)
将SQLAlchemey数据转换为JSON很简单,但我需要从请求中解析JSON,以便我可以追加/更新通过POST发送到RESTful API的数据。我知道如何处理非嵌套的JSON,但我会第一个承认我在处理嵌套JSON时对于属于关系结构中多个表的记录毫无头绪。
我试过没有任何运气的搜索高低。我能找到的最近的是here“使用flask-restful RequestParser进行嵌套验证”,但这并不是基于我的嵌套结构点击我需要做的事情。
答案 0 :(得分:4)
酷!看起来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"
}
}
发送到此终端:
@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)
我得到以下对象:
{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'}}}
更新:
使用此代码在我的终端中进行测试时,能够很好地访问我的所有词典项目:
# 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"]
回想起来,现在这似乎相当微不足道。我希望这可以帮助将来的某个人。
答案 1 :(得分:1)
您是否有权访问并可以编辑JSON?
一些编辑将有助于使其成为有效的 JSON:
{
和}
,
行country
醇>
如果你能这样做,你的JSON将如下所示:
{
"site": {
"id": "37251",
"site_name": "TestSite",
"address": {
"address": "1234BlaisdellAve",
"city": "Minneapolis",
"state": "MN",
"zip": "55456",
"neighborhood": "Kingfield",
"county": "Hennepin"
},
"geolocation": {
"latitude": "41.6544",
"longitude": "73.3322",
"accuracy": "45"
}
}
}
将其排序,使用Python的json
来处理它:
import json
file_handler = open('test.json', 'r')
parsed_data = json.loads(file_handler.read())
print parsed_data
输出是一个diciotnary,您可以轻松迭代以验证您的数据:
{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'1234BlaisdellAve'}}}
但如果您无法编辑JSON以使其语法更好,json.loads
将无法解析它......