Heim > Backend-Entwicklung > PHP-Tutorial > 这样的json数据如何解析

这样的json数据如何解析

WBOY
Freigeben: 2016-06-13 13:31:23
Original
1080 Leute haben es durchsucht

这样的json数据怎么解析

JScript code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

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']))

Nach dem Login kopieren

我用这种方法

$str = file_get_contents($url);
$data = json_decode($str);
得不到数据。
我主要是想遍历data里面的数据,得到里面的值。


------解决方案--------------------
主要是后面的 【'count' : 541, pagesize: 15,】格式不对,要处理一下
PHP code
<?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));
 <div class="clear">
                 
              
              
        
            
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage