首页 > 解决方案 > 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 格式?

标签: reactjsreact-nativereact-native-android

解决方案


你能试试这个代码吗?

constructor(props) {
    super(props);
    this.state =
    {
      selectedDropDownValue:{}
    };
  }

func(itemName,itemValue)
      this.setState({
          selectedDropDownValue: {"size": itemName, "color": itemValue}
      });
}

推荐阅读