JS JSON数组与字符串的相互转换 JSON.parse()与JSON.stringify()

  public todos: {
    id: number,
    title: string,
    done: boolean
  }[] = JSON.parse(window.localStorage.getItem('todos') || '[]')

  ngDoCheck(){
    window.localStorage.setItem('todos', JSON.stringify(this.todos))
  }
  • JSON.parse() 将字符串转化为JSON数组

  • JSON.stringify() 将JSON数组转化为字符串

原文地址:https://www.cnblogs.com/SharkJiao/p/13760820.html