小编Sch*_*tin的帖子

Python 将对象列表转换为 JSON 字符串

我正在尝试将 Python 对象列表解析为 Json 字符串。我有一个名为“list_mitarbeiter”的 Python 对象列表。在这个列表中是 Mitarbeiter 的对象:

class Mitarbeiter:
    def __init__(self, _id, name, nachname):
        self._id = _id
        self.name = name
        self.nachname = nachname
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下命令将 list_mitarbeiter 解析为 JSON 字符串:

x = json.dumps(z.__dict__ for z in list_mitarbeiter)
Run Code Online (Sandbox Code Playgroud)

每次我尝试这个时,我都会收到一个错误:类型生成器的对象不是 JSON 可序列化的

我已经只用列表的 1 个对象完成了它并且它起作用了,我不明白为什么当我用 for 循环为 list_mitarbeiter 中的每个对象做它时它不起作用。

X = json.dumps(list_mitarbeiter [1]。字典)^^这是工作

这是我的整个代码:

rom pymongo import MongoClient
from flask import Flask
import json

app = Flask(__name__)

class Mitarbeiter:
    def __init__(self, _id, name, nachname):
        self._id = _id
        self.name = name …
Run Code Online (Sandbox Code Playgroud)

python json

1
推荐指数
1
解决办法
1024
查看次数

Python Error SyntaxError: invalid syntax for no particular reason

I have a SyntaxError for no apparent reason. I know that Python sometimes gives the wrong line of Error. But I also checked the previous lines and i didn't find any wrong line.

import math
import numpy

class neuralNetwork:
        def __init__ (self,inputnodes,hiddennodes,outputnodes,learningrate):

            #set number of nodes in each input, hidden, output layer
            self.inodes= inputnodes
            self.hnodes= hiddennodes
            self.onodes= outputnodes

            ```
            self.wih = (numpy.random.normal(0.0,pow(self.hnodes,- 0.5), 
            (self.hnodes,self.inodes))
            self.who = (numpy.random.normal(0.0,pow(self.hnodes,- 0.5), 
            (self.hnodes,self.inodes))

            ```
            # learning rate
             elf.lr = learningrate

            pass
        def train():
            pass …
Run Code Online (Sandbox Code Playgroud)

python jupyter

-4
推荐指数
1
解决办法
45
查看次数

标签 统计

python ×2

json ×1

jupyter ×1