我在发送POST请求时创建了一个带有JSON对象的字符串数组,如下所示:
{ "inList" : [ { "smarter" : [ "Einstein", "Feynmann" ] },
{ "smarter" : [ "Feynmann","Gell-Mann"]},
{ "smarter": [ "Gell-Mann","Thorne"]},
{ "smarter" : [ "Einstein","Lorentz"]},
{ "smarter" : [ "Lorentz","Planck"]},
{ "smarter" : [ "Hilbert","Noether"]},
{ "smarter" : [ "Poincare","Noether"]}
]}
我试图对名称进行排序,以便出现在智能对的第一个元素中的任何人都会在响应输出中打印出他们的名字。
from flask import Flask
from flask import request
import json
app = Flask(__name__)
@app.route('/postjson', methods = ['POST'])
def postJsonHandler(self):
candidates = set()
nodes = {}
sorted = []
self.incoming = set()
self.outgoing = set()
if request.json:
smarter[] = json.loads(inList['smarter'])
for l in smarter[]:
p1,p2 = tuple([s.strip() for s in l.split(":")])
if p1 not in nodes:
nodes[p1] = Node()
if p2 not in nodes:
nodes[p2] = Node()
nodes[p1].outgoing.add(p2)
nodes[p2].incoming.add(p1)
if len(nodes[p1].incoming)==0:
candidates.add(p1)
candidates.discard(p2)
while len(candidates)>0:
candidate = candidates.pop()
sorted.append(candidate)
while len(nodes[candidate].outgoing)>0:
out = nodes[candidate].outgoing.pop()
nodes[out].incoming.remove(candidate)
if len(nodes[out].incoming)==0:
candidates.add(out)
print sortedhello
return sortedhello(['smarter'])
else:
return "no json"
app.run(host='0.0.0.0', port= 8090)