javascript - 如何在Javascript中将字符串转换为数组
问题描述
如何转换“[{"id":6,"name":"r6","preparation_time":"123","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:27:02","updated_at":"2020-09-11 09:27:02"},{"id":5,"name":"r5","preparation_time":"21","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:26:28","updated_at":"2020-09-11 09:26:28"}]"
至[{"id":6,"name":"r6","preparation_time":"123","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:27:02","updated_at":"2020-09-11 09:27:02"},{"id":5,"name":"r5","preparation_time":"21","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:26:28","updated_at":"2020-09-11 09:26:28"}]
解决方案
给定的字符串是 json 格式。通过使用JSON.parse()。
$arr = JSON.parse('[{"id":6,"name":"r6","preparation_time":"123","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:27:02","updated_at":"2020-09-11 09:27:02"},{"id":5,"name":"r5","preparation_time":"21","servings":4,"background_media_file":"recipe_background_image","font_color":"#000000","created_at":"2020-09-11 09:26:28","updated_at":"2020-09-11 09:26:28"}]');
console.log($arr);
推荐阅读
- swiftui - 带有 UIViewRepresentable 的 SwiftUI 上下文菜单的意外视觉行为
- java - 抽象对谁隐藏了实现?
- javascript - TypeError:无法读取 null 的属性“调度”
- matlab - MEX 文件中的意外标准异常(pytorch 模型转发)
- c++ - 同时按住两个键盘键更改我的 VS MFC 图形分配中的图形大小
- java - 给定一个 DSAPrivateKey,如何计算对应的 DSAPublicKey?
- amazon-s3 - 如何将 Athena 创建的 Parquet 表读入 python
- ruby-on-rails - 设计 authenticate_user 重定向到新的会话路径
- mysql - 如何解决匹配单反的sql问题?
- python - global statement isn't working, and I don't understand why