【问题标题】:Create Json object using flask使用烧瓶创建 Json 对象
【发布时间】:2019-03-08 20:13:15
【问题描述】:

我正在使用flask框架实现一个python API,这是我的代码:

    current_months = [this_month_list]
    result = pd.concat(current_months)
    my_array = np.array(result['city'])
    freqs = Counter(my_array)
    return jsonify(freqs)

我的问题在于创建 JSON 对象。 通过上面的代码,JSON 是这样的:

{
'Riyadh': 50
'Jeddah': 10
'Los Angeles': 30}

但是,我想在 JSON 对象中包含一条消息。 这是我想要达到的结果:

{
'Message' : "I want to include this message"
'Result' : {
    'Riyadh': 50
    'Jeddah': 10
    'Los Angeles': 30
}

}

【问题讨论】:

    标签: python json flask


    【解决方案1】:

    只需将它们附加到字典中:

    freqs = {
        'Message': 'Some msg',
        'Result': Counter(my_array)
    }
    return jsonify(freqs)
    

    【讨论】:

      【解决方案2】:

      所以只需包装您的数据:

      data = {
          'Message' : "I want to include this message"
          'Result' : freqs }
      return jsonify(data)
      

      【讨论】:

        猜你喜欢
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        • 2017-01-19
        相关资源
        最近更新 更多