使用 __json__ 的金字塔在客户端返回意外的格式

问题描述 投票:0回答:1

我正在设置 Pyramid 后端和 Angular 前端应用程序。

http GET 请求从 Angular 发送到 Pyramid。

在 Pyramid 中,类 (List()) 用于转换数据(使用 __json__ 函数)。

当 Pyramid 使用 json 渲染器响应 GET 请求时,Angular 端收到的数据与预期不符。 id 看起来像是 Array 类型,其中包含 id 的值...

以下是使用 __json__ 函数定义类 List 的代码:

class List():
    id = None
    def __init__(self, Id, Name, Image):
        self.id = Id,
        self.name = Name

    def __json__(self, request):
        return {
                'Id':self.id,
                'Name':self.name
                }

使用 json 渲染器进行 GET 请求的视图:


@view_config(route_name='lijsten', renderer='json', request_method='GET')
def lijsten_view(request):
    request.matchdict['lijstID'] == 'techniek':
        technieken = {
                       "01" : "Berliner wand" ,
                       "02" : "Secanspalen wand",
                       "03" : "Cutter Soil Mix wand",
                       "10" : "Avegaarpaal",
                       "11" : "Verbuisde Boorpaal",
                       "12" : "Grondverdingende Schroefpaal",
                       "13" : "Funderingsput",
                       "14" : "Micropaal"
                     }
        
        jsonlist = []
        for key, value in technieken.items():
            tech = List(str(key), str(value), '')
            jsonlist.append(tech)
        return jsonlist

在 Angular 端,get.Technieken() 用于发送 GET 请求:

    getTechnieken(): Observable<Techniek[]> {
        this.messageService.add('DataService: technieken aangevragen')
        const url = `${this.restUrl}/lijsten/techniek`;
        return this.http
            .get<Techniek[]>(url)
            .pipe(
            //tap(_ => this.log('fetched lijsten on: ' + url )),
            catchError(this.handleError<Techniek[]>('getTechnieken', []))
            );
    }

将返回的数组打印到控制台时,我得到以下信息:

Array(8) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…} ]
​
0: Object { Id: (1) […], Name: "Berliner wand" }
​​
    Id: Array [ "01" ]
​​​
       0: "01"
​​​
       length: 1
​​​
       <prototype>: Array []
​​
    Name: "Berliner wand"
​​
<prototype>: Object { … }
​
1: Object { Id: (1) […], Name: "Secanspalen wand" }
​
2: Object { Id: (1) […], Name: "Cutter Soil Mix wand" }
​
3: Object { Id: (1) […], Name: "Avegaarpaal" }
​
4: Object { Id: (1) […], Name: "Verbuisde Boorpaal" }
​
5: Object { Id: (1) […], Name: "Grondverdingende Schroefpaal" }
​
6: Object { Id: (1) […], Name: "Funderingsput" }
​
7: Object { Id: (1) […], Name: "Micropaal" }

我扩展了Array中的第一个对象,其中Id被标识为一个Array,它应该是一个字符串:Id:“01”...

根据 Pyramid 文档,预期输出如下所示:

from pyramid.view import view_config


class MyObject(object):

    def __init__(self, x):

        self.x = x


    def __json__(self, request):

        return {'x':self.x}


@view_config(renderer='json')

def objects(request):

    return [MyObject(1), MyObject(2)]


# the JSON value returned by ``objects`` will be:

#    [{"x": 1}, {"x": 2}]

我不清楚我做错了什么。 有人可以帮忙吗?

python json angular pyramid
1个回答
0
投票

在Python中,逗号创建一个元组,括号是可选的:

for name, value in blah:
相当于
for (name, value) in blah:
。另外
blah = 'Hello', 'there'
blah = ('Hello', 'there')

相同

所以,在你的 List 类中,

class List():
    id = None
    def __init__(self, Id, Name, Image):
        self.id = Id,
        self.name = Name

self.id被分配了一个只有一个元素的元组,与

self.id = (id,)
相同,从JSON的角度来看与
self.id = [id]
相同。

© www.soinside.com 2019 - 2024. All rights reserved.