我正在设置一个 pyramid 后端和一个棱角前端应用程序.

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打印到控制台时,我得到以下内容:

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" }

我展开了数组中的第一个对象,其中Id被标识为数组,它应该是一个字符串: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中,逗号创建一个元组,括号是可选的:for name, value in blah:等于for (name, value) in blah:.blah = 'Hello', 'there'blah = ('Hello', 'there')一样

在你的列表课上

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

self.id

Python相关问答推荐

从webhook中的短代码(而不是电话号码)接收Twilio消息

使用SciPy进行曲线匹配未能给出正确的匹配

需要计算60,000个坐标之间的距离

删除所有列值,但判断是否存在任何二元组

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

Julia CSV for Python中的等效性Pandas index_col参数

如何更改分组条形图中条形图的 colored颜色 ?

OR—Tools CP SAT条件约束

如何从数据库上传数据到html?

部分视图的DataFrame

在ubuntu上安装dlib时出错

不允许访问非IPM文件夹

无法连接到Keycloat服务器

如何在Python中获取`Genericums`超级类型?

在matplotlib中删除子图之间的间隙_mosaic

在输入行运行时停止代码

如何从一个维基页面中抓取和存储多个表格?

如何在表单中添加管理员风格的输入(PDF)

如何在Polars中将列表中的新列添加到现有的数据帧中?