2013-05-03 115 views
-2
bfunc({ 
"query": { 
    "count": 1, 
    "created": "2013-05-03T06:20:01Z", 
    "lang": "en-US", 
    "diagnostics": { 
    "publiclyCallable": "true", 
    "cache": {  
    "execution-start-time": "32", 
    "execution-stop-time": "32", 
    "execution-time": "0", 
    "method": "GET", 
    "type": "MEMCACHED", 
    "content": "http://www.vtualerts.com/robots.txt" 
    }); 

這是我需要使用javascript和jQuery解析的JSON數據。我試圖做這樣的......使用Javascript和jQuery解析JSON

<div id="placeholder"></div>  
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<script> 
$.getJSON('myjson.json', function(data) {  
    var output= data.cbfunc.query.count+ "" + data.cbfunc.query.created;  
    document.getElementById("placeholder").innerHTML=output;  
}); 

但我得到一個錯誤

的XMLHttpRequest無法加載文件:///home/shivratna/Desktop/myjson.json。 Access-Control-Allow-Origin不允許Origin null。

請大家幫忙。

真誠的感謝

+0

URL正確嗎? – 2013-05-03 07:04:44

+0

嘗試放置警報(「data:」+ JSON.stringify(data));也許你正在獲取數據。通過放置這條線進行驗證。 – Anil 2013-05-03 07:05:08

+4

'bfunc(...)'不是有效的JSON。這甚至不是有效的JSONP。 – 2013-05-03 07:05:50

回答

1

這不是有效的JSON。請閱讀json.org瞭解JSON的正確結構。你有什麼是JSONP。在別的之前,請檢查您的JSON,因爲它們沒有正確關閉。你缺少大括號。

把它變成有效的JSON,取出填充功能,像這樣:你爲什麼用這樣的JSON數據

{ 
    "query": { 
    "count": 1, 
    "created": "2013-05-03T06:20:01Z", 
    "lang": "en-US", 
    "diagnostics": { 
     "publiclyCallable": "true", 
     "cache": {  
     "execution-start-time": "32", 
     "execution-stop-time": "32", 
     "execution-time": "0", 
     "method": "GET", 
     "type": "MEMCACHED", 
     "content": "http://www.vtualerts.com/robots.txt" 
     } 
    } 
    } 
} 
+1

'1'和'「1」'也是有效的JSON。即使'null'也是有效的JSON – 2013-05-03 07:11:53

+0

@JanDvorak在他的情況下,它應該以'{}'開始,但無論如何,我會重述。 – Joseph 2013-05-03 07:12:51

-1

,這是無效的,你可以使用JSON數據,如

{ "query": { 
"count": 1, 
"created": "2013-05-03T06:20:01Z", 
"lang": "en-US", 
"diagnostics": { 
    "publiclyCallable": "true", 
    "cache": { 
     "execution-start-time": "32", 
     "execution-stop-time": "32", 
     "execution-time": "0", 
     "method": "GET", 
     "type": "MEMCACHED", 
     "content": "http://www.vtualerts.com/robots.txt" 
    } 
} 
} 
} 

您的最終代碼..

$.getJSON('myjson.json', function(data) {  
     var output= data.query.count+ "" + data.query.created;  
     document.getElementById("placeholder").innerHTML=output;  
    }); 
+3

你確定嗎? – Joseph 2013-05-03 07:15:35

+0

關於json數據? – praveen 2013-05-03 07:22:15

+0

Thnax每個人都爲你提供幫助。其實我從YQL控制檯獲取這個json數據。我不是自己寫的。 – 2013-05-03 07:27:41