将JSON字符串解析为数组 [英] Parse JSON string into an array

查看:133
本文介绍了将JSON字符串解析为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JSON解析字符串并将这些元素转换为Javascript中的数组。这是代码。

I'm trying to parse a string from JSON and turn those elements into an array in Javascript. Here's the code.

      var data = "{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":"0,1,2,3,"}";
      var getDay = data.day0;
      var getDayArray = getDay.split(",");

基本上,我正在尝试获取day0,即0,1,2,3,以及把它变成一个结构为

Essentially, I'm trying to get day0, which is 0,1,2,3, and turn it into an array with a structure of

[0] = 0
[1] = 1
[2] = 2
[3] = 3

什么是最好的方法要做这个吗?

What is the best way to go about doing this?

推荐答案

这样的事情。这个尾随逗号有意吗?

Something like this. Is that trailing comma intentional?

var getDayArray = JSON.parse(data).day0.split(",")

这篇关于将JSON字符串解析为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆