JSON转换为JS对象和JS对象转换为JSON

1. JSON转换为JS对象

  • 名称

    • parse
  • 类型

    • 执行类
  • 参数

    • jsonstring JSON字符串
  • 返回值

    • js对象
  • 示例

parse('{"a":"abc","b":true,"c":123,"d":{"e":"test"},"e":[1,2,"3"]}');
// { a: 'abc', b: true, c: 123, d: { e: 'test' }, e: [1, 2, '3'] }

parse('[1,2,3]');
// [1, 2, 3]

parse('"abc"');
// 'abc'

parse('123');
// 123

parse('true');
// true

1. JS对象转换为JSON

  • 名称

    • stringify
  • 类型

    • 执行类
  • 参数

    • value:any JS对象
  • 返回值

    • string JSON格式的字符串
  • 示例

stringify({ a: 'abc', b: true, c: 123, d: { e: 'test' }, e: [1, 2, '3'] });
// '{"a":"abc","b":true,"c":123,"d":{"e":"test"},"e":[1,2,"3"]}'

stringify([1, 2, 3]);
// '[1,2,3]'

stringify('abc');
// '"abc"'

stringify(123);
// '123'

stringify(false);
// 'false'

你可能感兴趣的:(js)