求大神解答这个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个回答

 $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
                )

        )

)


qq_22916833
qq_22916833 谢谢
5 年多之前 回复

你这不是标准的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>
qq_22916833
qq_22916833 恩是的 原来是js 我想转 json 感谢回答
5 年多之前 回复

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

xuzuning大神也来玩问答啦

json_decode

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问