简体   繁体   English

flask.jsonify 返回带有方括号而不是大括号的数组

[英]flask.jsonify returns array with square brackets instead of curly brackets

I am using flask and jsonify for the first time and i am stuck at a small problem.我第一次使用 flask 和 jsonify,我遇到了一个小问题。 My json output is returning an array format with square brackets instead of the json object with curly brackets.我的 json output 返回带有方括号的数组格式,而不是带有大括号的 json ZA8CFDE6331BD59EB2AC96F89111.C4。

Can someone pls point me in the right direction?有人可以指出我正确的方向吗?

My function takes a text and using spacy breaks it down into tokens and details about the tokens.我的 function 获取文本并使用 spacy 将其分解为令牌和有关令牌的详细信息。

my code is-我的代码是-

@app.route('/api/<string:mytext>',methods=['GET'])
def myfunc(mytext):

    docx = nlp(mytext.strip())
    allData = ['Token:{},Tag:{},POS:{}'.format(token.text,token.tag_,token.pos_) for token in docx ]
    
    return jsonify(allData)

It returns the data as它将数据返回为

[
  "Token:\",Tag:``,POS:PUNCT", 
  "Token:test,Tag:VB,POS:VERB", 
  "Token:this,Tag:DT,POS:DET", 
]

I want the return JSON to be the standard json response with curly brackets so my c# app can deserialise it properly.我希望返回 JSON 成为标准的 json 响应,带有大括号,以便我的 c# 应用程序可以正确反序列化它。

Any help is appreciated.thanks任何帮助表示赞赏。谢谢

You want your list comprehension to create a Python dict /curly brackets.您希望您的列表理解创建一个 Python dict / 大括号。 It'll still need to be created as a list /Array/square brackets as your key names are the same for each line/entity.它仍然需要创建为list /Array/方括号,因为您的键名对于每行/实体都是相同的。

allData = [{'Token': token.text, 'Tag': token.tag_, 'POS': token.pos_} for token in docx]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM