【问题标题】:GAE python27 return nested jsonGAE python27返回嵌套的json
【发布时间】:2017-04-26 20:51:40
【问题描述】:

这似乎是一个简单的任务,但它却让我难以捉摸......

class ViewAllDogs(webapp2.RequestHandler):
    """ Returns an array of json objects representing all dogs. """
    def get(self):
        query = Dog.query()
        results = query.fetch(limit = MAX_DOGS)   # 100
        aList = []
        for match in results:
            aList.append({'id': match.id, 'name': match.name,
                           'owner': match.owner, arrival_date':match.arrival_date})
            aList.append({'departure_history':{'departure_date': match.departure_date,
                          'departed_dog': match.departed_dog}})
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(aList))

以上是我迄今为止最好的尝试,让我明白了:

[
  {
    "arrival_date": null,
    "id": "a link to self",
    "owner": 354773,
    "name": "Rover"
  },
  {
    "departure_history": {
      "departed_dog": "Jake",
      "departure_date": 04/24/2017
    }
  },

 # json array of objects continues...
]

我想要得到的是嵌套的离开历史:

[
  {
    "id": "a link to self...",
    "owner": 354773,
    "name": "Rover",
    "departure_history": {
      "departed_dog": "Jake",
      "departure_date": 04/24/2017
      },
    "arrival_date": 04/25/2017,
  },

# json array of objects continues...
]

我尝试了很多不同的组合,查看了 json 文档、python27 文档,没有任何乐趣,并且为此花费了太多时间。我已经通过有关该主题的许多相关 SO 帖子了解了这一点。提前致谢。

【问题讨论】:

    标签: json google-app-engine gae-python27


    【解决方案1】:

    你可以简化一点:

            aList = []
            for match in results:
                aDog = {'id': match.id, 
                        'name': match.name, 
                        'owner': match.owner, 
                        'arrival_date':match.arrival_date,
                        'departure_history': {
                            'departure_date': match.departure_date,
                            'departed_dog': match.departed_dog}
                       }
                aList.append(aDog)
    

    【讨论】:

      【解决方案2】:

      这似乎有点骇人听闻,但它确实有效。如果你知道更好的方法,一定要告诉我。谢谢。

       class ViewAllDogs(webapp2.RequestHandler):
              """ Returns an array of json objects representing all dogs. """
              def get(self):
                  query = Dog.query()
                  results = query.fetch(limit = MAX_DOGS)   # 100
                  aList = []
                  i = 0
                  for match in results:
                      aList.append({'id': match.id, 'name': match.name,
                                     'owner': match.owner, arrival_date':match.arrival_date})
                      aList[i]['departure_history'] = ({'departure_history':{'departure_date': match.departure_date,
                                    'departed_dog': match.departed_dog}})
                   i += 1
                  self.response.headers['Content-Type'] = 'application/json'
                  self.response.write(json.dumps(aList))
      

      【讨论】:

        猜你喜欢
        • 2020-10-23
        • 2017-03-15
        • 1970-01-01
        • 2015-02-03
        • 1970-01-01
        • 2022-12-04
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多