var myTransActionsData = {'data': [
 {
'stock_name': "\u7231\u4ed5\u8fbe",
'stock_symbol': '002403',
'transaction_time': '2010-09-10 09:32:31',
'flag': 'sell',
'price': '17.52',
'amount': '39900',
'display': 'black'
},
 {
'stock_name': "\u7231\u4ed5\u8fbe",
'stock_symbol': '002403',
'transaction_time': '2010-09-09 13:05:16',
'flag': 'buy',
'price': '17.65',
'amount': '39900',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-09-07 10:53:08',
'flag': 'sell',
'price': '84.98',
'amount': '8300',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-09-06 13:01:47',
'flag': 'buy',
'price': '79.80',
'amount': '8300',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-09-03 09:31:08',
'flag': 'sell',
'price': '78.87',
'amount': '8300',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-09-02 09:37:44',
'flag': 'buy',
'price': '75.54',
'amount': '8300',
'display': 'black'
},
 {
'stock_name': "\u6d77\u683c\u901a\u4fe1",
'stock_symbol': '002465',
'transaction_time': '2010-09-02 09:30:27',
'flag': 'sell',
'price': '49.10',
'amount': '13000',
'display': 'black'
},
 {
'stock_name': "\u6d77\u683c\u901a\u4fe1",
'stock_symbol': '002465',
'transaction_time': '2010-09-01 14:01:05',
'flag': 'buy',
'price': '49.54',
'amount': '13000',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-09-01 13:46:08',
'flag': 'sell',
'price': '72.98',
'amount': '8800',
'display': 'black'
},
 {
'stock_name': "\u987a\u7f51\u79d1\u6280",
'stock_symbol': '300113',
'transaction_time': '2010-08-27 09:34:52',
'flag': 'buy',
'price': '64.16',
'amount': '8800',
'display': 'black'
},
 {
'stock_name': "\u91d1\u53d1\u79d1\u6280",
'stock_symbol': '600143',
'transaction_time': '2010-08-27 09:30:59',
'flag': 'sell',
'price': '10.99',
'amount': '52100',
'display': 'black'
},
 {
'stock_name': "\u91d1\u53d1\u79d1\u6280",
'stock_symbol': '600143',
'transaction_time': '2010-08-26 13:59:58',
'flag': 'buy',
'price': '10.92',
'amount': '52100',
'display': 'black'
},
 {
'stock_name': "\u534e\u4ec1\u836f\u4e1a",
'stock_symbol': '300110',
'transaction_time': '2010-08-26 10:35:36',
'flag': 'sell',
'price': '22.57',
'amount': '25300',
'display': 'black'
},
 {
'stock_name': "\u534e\u4ec1\u836f\u4e1a",
'stock_symbol': '300110',
'transaction_time': '2010-08-25 09:31:05',
'flag': 'buy',
'price': '23.88',
'amount': '25300',
'display': 'black'
},
 {
'stock_name': "\u90d1\u7164\u673a",
'stock_symbol': '601717',
'transaction_time': '2010-08-24 10:41:38',
'flag': 'sell',
'price': '32.63',
'amount': '18400',
'display': 'black'
}],
'count' : 541,
pagesize: 15,
'status' : '1000'
}myTransActionsData['pages'] = Math.ceil(parseInt(myTransActionsData['count'])/parseInt(myTransActionsData['pagesize']))我用这种方法$str = file_get_contents($url);
$data = json_decode($str);
得不到数据。
我主要是想遍历data里面的数据,得到里面的值。

解决方案 »

  1.   

    你要把多余的代码都过滤掉,,只剩下JSON数据,才能用json_decode()的啊
      

  2.   

    你的json数据放在哪? 通过 $str = file_get_contents($url);就能获取到吗?
      

  3.   

    http://app.vstock.jrj.com.cn/api/myTransaction/?uid=091220010012659154&size=15&page=19&exp_stock=&created_start=&created_end=&t=1337046299.426
      

  4.   

    file_get_contents 取到内容后,
    把第一行的“var myTransActionsData = ”取掉,
    把最后一行“myTransActionsData['pages'] = Math.ceil(parseInt(myTransActionsData['count'])/parseInt(myTransActionsData['pagesize'])) ”去掉,
    再用 json_decode 就可以了
      

  5.   

    $str = preg_replace('/\"/','\'',$str);
    preg_match('/=(.*)myTransActionsData/isU',$str,$match);
    $data = json_decode($match[1]);
    不得行。
      

  6.   

    格式也不行吧。json_decode单引不行。print_r(json_decode("{'aa':'test','bb':[1,2,3]}"));
    需要这样的格式:print_r(json_decode('{"aa":"test","bb":[1,2,3]}'));
    最简单的方法就是用js
    <script src="http://app.vstock.jrj.com.cn/api/myTransaction/?uid=091220010012659154&size=15&page=19&exp_stock=&created_start=&created_end=&t=1337046299.426"></script>然后再 
    for(var k in myTransActionsData['data'])
    {
      // myTransActionsData[k].stock_name 等
    }
      

  7.   

    主要是后面的 【'count' : 541, pagesize: 15,】格式不对,要处理一下
    <?php
    $str=<<<JSON
    { 'data': [ { 'stock_name': "\u7231\u4ed5\u8fbe", 'stock_symbol': '002403', 'transaction_time': '2010-09-10 09:32:31', 'flag': 'sell', 'price': '17.52', 'amount': '39900', 'display': 'black' }, { 'stock_name': "\u7231\u4ed5\u8fbe", 'stock_symbol': '002403', 'transaction_time': '2010-09-09 13:05:16', 'flag': 'buy', 'price': '17.65', 'amount': '39900', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-09-07 10:53:08', 'flag': 'sell', 'price': '84.98', 'amount': '8300', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-09-06 13:01:47', 'flag': 'buy', 'price': '79.80', 'amount': '8300', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-09-03 09:31:08', 'flag': 'sell', 'price': '78.87', 'amount': '8300', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-09-02 09:37:44', 'flag': 'buy', 'price': '75.54', 'amount': '8300', 'display': 'black' }, { 'stock_name': "\u6d77\u683c\u901a\u4fe1", 'stock_symbol': '002465', 'transaction_time': '2010-09-02 09:30:27', 'flag': 'sell', 'price': '49.10', 'amount': '13000', 'display': 'black' }, { 'stock_name': "\u6d77\u683c\u901a\u4fe1", 'stock_symbol': '002465', 'transaction_time': '2010-09-01 14:01:05', 'flag': 'buy', 'price': '49.54', 'amount': '13000', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-09-01 13:46:08', 'flag': 'sell', 'price': '72.98', 'amount': '8800', 'display': 'black' }, { 'stock_name': "\u987a\u7f51\u79d1\u6280", 'stock_symbol': '300113', 'transaction_time': '2010-08-27 09:34:52', 'flag': 'buy', 'price': '64.16', 'amount': '8800', 'display': 'black' }, { 'stock_name': "\u91d1\u53d1\u79d1\u6280", 'stock_symbol': '600143', 'transaction_time': '2010-08-27 09:30:59', 'flag': 'sell', 'price': '10.99', 'amount': '52100', 'display': 'black' }, { 'stock_name': "\u91d1\u53d1\u79d1\u6280", 'stock_symbol': '600143', 'transaction_time': '2010-08-26 13:59:58', 'flag': 'buy', 'price': '10.92', 'amount': '52100', 'display': 'black' }, { 'stock_name': "\u534e\u4ec1\u836f\u4e1a", 'stock_symbol': '300110', 'transaction_time': '2010-08-26 10:35:36', 'flag': 'sell', 'price': '22.57', 'amount': '25300', 'display': 'black' }, { 'stock_name': "\u534e\u4ec1\u836f\u4e1a", 'stock_symbol': '300110', 'transaction_time': '2010-08-25 09:31:05', 'flag': 'buy', 'price': '23.88', 'amount': '25300', 'display': 'black' }, { 'stock_name': "\u90d1\u7164\u673a", 'stock_symbol': '601717', 'transaction_time': '2010-08-24 10:41:38', 'flag': 'sell', 'price': '32.63', 'amount': '18400', 'display': 'black' } ], 'count' : 541, pagesize: 15, 'status' : '1000' } 
    JSON;
    $str=str_replace('\'','"',$str);
    $str=preg_replace('/"\s*:([^"]+),\s*"/',"\":\"$1\",\"",$str);print_r(json_decode($str,true));
      

  8.   

    $str=preg_replace('/"\s*:([^"]+),\s*"/',"\":\"$1\",\"",$str);这一句话的意思是?
      

  9.   

    把类似于这种格式【"count": 541, pagesize: 15,】的部分处理成这样【"count" : "541, pagesize: 15",】
    上面有点局限 修改一下  $str=preg_replace('/"\s*:([^"\]}]+)([,\]}])/','":"$1"$2',$str);
      

  10.   

    你的原码可以
    但是我用file_get_content  打开连接 得到的字符串就是不行。
    $url = 'http://app.vstock.jrj.com.cn/api/myTransaction/?uid=091220010012659154&size=15&page=19&exp_stock=&created_start=&created_end=&t=1337046299.426';
    $str = file_get_contents($url);
    $str=str_replace('\'','"',$str);
    preg_match('/=(.*)myTransActionsData/isU',$str,$match);
    $str=preg_replace('/"\s*:([^"\]}]+)([,\]}])/','":"$1"$2',$match[1]);
    //$str=preg_replace('/"\s*:([^"]+),\s*"/',"\":\"$1\",\"",$match[1]);
    print_r(json_decode($str.true));exit();
    我json_encode看了一下,多了几个\n
    但是我用$str = preg_replace('/n','',$str);
    还是去不掉。
      

  11.   

    <?php
    header("Content-Type:text/html; Charset=UTF-8");$str = file_get_contents('http://app.vstock.jrj.com.cn/api/myTransaction/?uid=091220010012659154&size=15&page=19&exp_stock=&created_start=&created_end=&t=1337046299.426');preg_match('/\'data\': (.*),\s\'count\' : (\d*),\spagesize: (\d*),\s\'status\' : \'(\d*)\'/isU',$str,$match);$str = trim($match[1]);
    $str = str_replace("'", '"', $str);$data = json_decode($str);echo "<br />data:";
    print_r($data);echo "<br />count:" . $match[2];echo "<br />pagesize:" . $match[3];echo "<br />status:" . $match[4];
    ?>
    这样就取到全部值了~
      

  12.   

    本帖最后由 xuzuning 于 2012-05-15 12:27:42 编辑
      

  13.   

    LZ试试我的方法 要遍历Json数据 首先要搞懂它的结构 伱的Json格式是下面这样的:var myTransActionsData = {'data':[{ , , },{},{}],'count' : 541,pagesize: 15,'status' : '1000'};搞懂了结构 循环就简单了!它是三个嵌套 所以三个循环就搞定了。下面我用for循环演示:for(var key in myTransActionsData){    obj=eval("myTransActionsData."+key);
        for(var i=0; i<obj.length; i++){
            for(var key in obj[i]){
                document.write(key+":"+obj[i][key]+",");
            }
            document.write("<br>");
        }
    }
    我试过了可以循环出来! 伱直接复制就可以用了!
      

  14.   

    myTransActionsData 这是个js对象啊,它和json有毛关系,用:var jsonText = JSON.stringify(myTransActionsData);把对象转成JSON串发给服务器啊。
      

  15.   

    告诉楼主一个很简单遍历Json的方法:那就是见到{}就for(var key in <??名字>);
    见到[]就for(var i=0; i<?.length; i++)
      

  16.   

    sorry......忘记后面这几个数据了'count' : 541,pagesize: 15,'status' : '1000 !!!!
    在obj后面加一行就OK!!
    for(var key in myTransActionsData){    obj=eval("myTransActionsData."+key);
        document.write(key+":"+myTransActionsData[key]+"<br>");   //这行新加的
        for(var i=0; i<obj.length; i++){

            for(var key in obj[i]){
                document.write(key+":"+obj[i][key]+",");
            }
            document.write("<br>");
        }

    }
      

  17.   

    <?php
    $url='http://app.vstock.jrj.com.cn/api/myTransaction/?uid=091220010012659154&size=15&page=19&exp_stock=&created_start=&created_end=&t=1337046299.426';
    $str=file_get_contents($url);
    $str=str_replace('\'','"',$str);
    preg_match('/=(.*)myTransActionsData/isU',$str,$match);
    $str=preg_replace('/"\s*:([^"\]}]+)([,\]}])/','":"$1"$2',$match[1]);
    $str=preg_replace('/\r|\n/','',$str);print_r(json_decode($str,true));