Parse JSON string into an array

前端 未结 3 1710
别那么骄傲
别那么骄傲 2021-01-03 04:52

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         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 05:25

    Most modern browsers have support for JSON.parse(). You would use it thusly:

      var dataJSON = '{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":"0,1,2,3"}'; // You need to remove the trailing comma
      var data = JSON.parse(dataJSON);
      var getDay = data.day0;
      var getDayArray = getDay.split(",");
    

    However, it might be better to modify whatever is generating the value for dataJSON, to return

      var dataJSON = '{"fname":"Todd","lname":"James","cascade":"tjames","loc":"res","place":"home", "day0":[0,1,2,3]}';
    

提交回复
热议问题