我正在开发一个带有React本地前端和Flask服务器的项目。我需要实现的事情之一是在客户端使用从服务器获取的信息创建一个列表。我已经将请求发送到我的服务器,服务器创建了一个python字典,其中包含我需要显示的信息,并将其转换为JSON对象,然后将其返回给我的客户机。你知道吗
classlist = {}
#for all instances that the user appeared in the has_class table
for i in range(len(hasclass)):
classId = hasclass[i].class_id
classTaken = Classes.query.filter_by(class_id=classId).first()
className = classTaken.class_name
classlist[i] = {}
classlist[i]["classname"] = className
classlist[i]["id"] = i
jsonclass = json.dumps(classlist)
return jsonclass
当客户端接收到它时,它被放入一个数组中。你知道吗
this.setState({data: [JSON.stringify(responseJson)]});
如果我打印出来此.state.data,我明白了
{{"0":{"classname":"testclass"},"1":{"classname":"classtest"}}
我尝试遍历这个对象,看看是否可以使用方括号进入它
this.state.data[0]["0"]["classname"]
和点表示法(但是这不起作用,因为键是数字)
this.state.data[0].0.classname
我试过把类名打印出来
<FlatList
data={Object.keys(this.state.data)}
renderItem={({ item }) => <Text>{this.state.data[item].classname}</Text>}
/>
在呈现平面列表时,我也尝试过使用keyExtractor函数,但到目前为止,我还没能让它在我的场景中工作。你知道吗
我已经试着让这个工作了2天,并尝试了一切我觉得有用的网上发现,但到目前为止没有任何工作。我想这和钥匙有关,但我不确定。你知道吗
有人能告诉我如何在React原生平面列表中显示对象类名吗?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐