reactjs - react-native:如何将 Object.assign 的输出转换为有效的 json 格式?
问题描述
constructor(props) {
super(props);
this.state =
{
selectedDropDownValue:[]
};
}
func(itemName,itemValue)
this.setState({
selectedDropDownValue: Object.assign(this.state.selectedDropDownValue, {[itemName]: itemValue})
});
}
和
func("size",null);
func("color","white");
当我记录它时,我得到这样的东西:
console.log(this.state.selectedDropDownValue);
[尺寸:空,颜色:“白色”]
我想要这样的东西:
{“尺寸”:空,“颜色”:“白色”}
我想将它发送到 PHP 服务器,但它不是有效的 JSON 代码。
如何在将其发送到服务器之前将其转换为 JSON 格式?
解决方案
你能试试这个代码吗?
constructor(props) {
super(props);
this.state =
{
selectedDropDownValue:{}
};
}
func(itemName,itemValue)
this.setState({
selectedDropDownValue: {"size": itemName, "color": itemValue}
});
}
推荐阅读
- android - 如何将 Firebase 旧适配器方法转换为新适配器方法?
- python - 将大 xml 拆分成小块
- php - 从本地主机上的 Angular 8 向服务器 API 发送 POST 请求
- excel - 有没有办法提示宏的用户输入他们自己的日期范围?
- android - Android 6 (Marshmallow) 默认启用 JIT 吗?
- ssl - 使用 Traefik / K8s 安装 2 个自定义 TLS 证书
- excel - 比较数据透视表数据
- c# - 从 c# 读取 3DSOLID 属性
- javascript - 在 ReactJs 中通过拖放交换两个图像
- highcharts - What is the highchart pie data structure