qq_22916833
2015-04-27 09:41
采纳率: 100%
浏览 1.7k
已采纳

求大神解答这个json如何转为数组

求大神解答这个json如何转为数组

 {trend : 3,yearLowPrice : 99.9,points : [['2014-10-29',139.9],['2014-11-11',115.9],['2014-11-12',139.9],['2014-12-12',129],['2014-12-13',139.9],['2015-01-01',119],['2015-01-06',139.9],['2015-02-12',159.9],['2015-02-27',139.9],['2015-03-06',119],['2015-03-08',139.9],['2015-03-23',99.9],['2015-03-27',139.9],['2015-04-27',139.91]]}

找在线校验工具老提示错误

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • xuzuning 2015-04-27 09:47
    已采纳
     $s =<<< TXT
    {trend : 3,yearLowPrice : 99.9,points : [['2014-10-29',139.9],['2014-11-11',115.9],['2014-11-12',139.9],['2014-12-12',129],['2014-12-13',139.9],['2015-01-01',119],['2015-01-06',139.9],['2015-02-12',159.9],['2015-02-27',139.9],['2015-03-06',119],['2015-03-08',139.9],['2015-03-23',99.9],['2015-03-27',139.9],['2015-04-27',139.91]]}
    TXT;
    $s = preg_replace_callback('/[\w.-]+/', function($m) { return '"'.$m[0].'"'; }, str_replace("'", '', $s));
    $a = json_decode($s, true);
    print_r($a);
    
    
     Array
    (
        [trend] => 3
        [yearLowPrice] => 99.9
        [points] => Array
            (
                [0] => Array
                    (
                        [0] => 2014-10-29
                        [1] => 139.9
                    )
    
                [1] => Array
                    (
                        [0] => 2014-11-11
                        [1] => 115.9
                    )
    
                [2] => Array
                    (
                        [0] => 2014-11-12
                        [1] => 139.9
                    )
    
                [3] => Array
                    (
                        [0] => 2014-12-12
                        [1] => 129
                    )
    
                [4] => Array
                    (
                        [0] => 2014-12-13
                        [1] => 139.9
                    )
    
                [5] => Array
                    (
                        [0] => 2015-01-01
                        [1] => 119
                    )
    
                [6] => Array
                    (
                        [0] => 2015-01-06
                        [1] => 139.9
                    )
    
                [7] => Array
                    (
                        [0] => 2015-02-12
                        [1] => 159.9
                    )
    
                [8] => Array
                    (
                        [0] => 2015-02-27
                        [1] => 139.9
                    )
    
                [9] => Array
                    (
                        [0] => 2015-03-06
                        [1] => 119
                    )
    
                [10] => Array
                    (
                        [0] => 2015-03-08
                        [1] => 139.9
                    )
    
                [11] => Array
                    (
                        [0] => 2015-03-23
                        [1] => 99.9
                    )
    
                [12] => Array
                    (
                        [0] => 2015-03-27
                        [1] => 139.9
                    )
    
                [13] => Array
                    (
                        [0] => 2015-04-27
                        [1] => 139.91
                    )
    
            )
    
    )
    
    
    
    
    打赏 评论
  • danielinbiti 2015-04-27 09:46

    你这不是标准的json格式,json格式key和value都用英文双引号括起来。

     <script>
    var a={trend : 3,yearLowPrice : 99.9,points : [['2014-10-29',139.9],['2014-11-11',115.9],['2014-11-12',139.9],['2014-12-12',129],['2014-12-13',139.9],['2015-01-01',119],['2015-01-06',139.9],['2015-02-12',159.9],['2015-02-27',139.9],['2015-03-06',119],['2015-03-08',139.9],['2015-03-23',99.9],['2015-03-27',139.9],['2015-04-27',139.91]]};
    alert(a['trend']);
    for(var i=0;i<a['points'].length;i++){
       console.log(a['points'][i][0]+","+a['points'][i][1]);
    }
    </script>
    
    打赏 评论
  • 睡梦之龙 2015-04-27 15:47

    json的键是可以不用引号的。

    打赏 评论
  • Chrisdowson 2015-04-28 01:12

    xuzuning大神也来玩问答啦

    打赏 评论
  • __Existence 2015-04-30 09:51

    json_decode

    打赏 评论

相关推荐 更多相似问题