2014-05-06 53 views
2

我正在使用返回jsonp字符串的crossdomain api。我想將它解析成一個JavaScript對象,以便更容易處理。 我知道有一個JSON字符串你可以這樣做:我如何解析jsonp到javascript對象?

success: function (val) { 
        var result = JSON.parse(val); 
       } 

但是,如果我這樣做與JSONP我從API得到,我得到「未捕獲的SyntaxError:意外的標記○」

我是做錯了,或者這是不是用jsonp做到這一點?

--------編輯1 --------------------------- 這就是我的jsonp字符串看起來像如果我打開它:

Object {resource: "boxscore", parameters: Object, resultSets: Array[22]} 
parameters: Object 
resource: "boxscore" 
resultSets: Array[22] 
0: Object 
1: Object 
2: Object 
3: Object 
4: Object 
headers: Array[28] 
    0: "GAME_ID" 
    1: "TEAM_ID" 
    2: "TEAM_ABBREVIATION" 
    3: "TEAM_CITY" 
    4: "PLAYER_ID" 
    5: "PLAYER_NAME" 
    6: "START_POSITION" 
    7: "COMMENT" 
length: 28 
__proto__: Array[0] 
name: "PlayerStats" 
rowSet: Array[26] 
    0: Array[28] 
     0: "0041300201" 
     1: 1610612764 
     2: "WAS" 
     3: "Washington" 
     4: 2772 
     5: "Trevor Ariza" 
     6: "F" 
     7: "" 
     8: "37:20" 
     9: 7 
     10: 10 
     11: 0.7 
     12: 6 
1: Array[28] 
2: Array[28] 
3: Array[28] 
4: Array[28] 
5: Array[28] 
6: Array[28] 

所以我想要做的是解析每個數組中的標題信息的數據,我該怎麼做? 因此,例如,如果我想GAME_ID我只寫GAME_ID,我得到每個數組的gameid「0041300201」。

+0

你是否console.log你的val變量? – lascort

+0

是的,我已在console.log中檢查過它。 – AshishHashCoder

+0

和它是什麼日誌.... – lascort

回答

1

可以使用JSONP liberary吧..,這將是一件好事,如果跨域網站給JSON刺痛:

go to this link it may be helpful. 

http://www.jquery4u.com/function-demos/jsonp/ 

or you can use the code like 


    /* Loading JSON objects using JSONP */ 
    (function($) { 
     var url = 'http://www.jquery4u.com/scripts/jquery4u.settings.json'; 
     $.ajax({ 
      type: 'GET', 
      url: url, 
      async: false, 
      contentType: "application/json", 
      dataType: 'jsonp' 
     }); 
    })(jQuery); 
+0

我如何處理我的變量?我把它作爲數據發送到該網址? –

+0

檢查我的第二個答案它將解決你的查詢。 – AshishHashCoder

+1

你爲什麼設置一個contentType?爲什麼你將異步設置爲false?這些東西都不會對jsonp請求產生任何影響。 –

-2

可能,這將解決您的問題:

function jsonp(data) { 
    alert('jsonp was called'); 
} 

$.ajax({ 
    url: 'http://jsfiddle.net/echo/jsonp/?callback=jsonp', 
    dataType: 'jsonp', 
    data: { 
    'foo': 'bar' 
    }, 
    success: function(data) { 
    console.log(data); 
    } 
}); 

實際上,JSONP根本就不是JSON,它只是JavaScript代碼。在這種情況下沒有理由去評估......你可以簡單地在全局定義該函數之後進行評估。 jQuery的同時也提供製作同域JSONP請求與特定JSONP回調的方式,如果你必須使用jQuery做

// Create the function the JSON data will be passed to. 
function myfunc(json) { 
    alert(json); 
} 

$.ajax({ 
    type: "GET", 
    url: "http://example.com?keyword=r&callback=jsonp", 
    dataType: 'jsonp', 
    jsonpCallback: 'myfunc', // the function to call 
    jsonp: 'callback', // name of the var specifying the callback in the request 
    error: function (xhr, errorType, exception) { 
    var errorMessage = exception || xhr.statusText; 
    alert("Excep:: " + exception + "Status:: " + xhr.statusText); 
    } 
}); 
+0

如果你正在使用jQuery,你不需要定義一個函數,jquery會爲你做這件事(這樣做可能會導致jquery拋出錯誤)。你只是使事情複雜化。 –

+0

@KevinB看到我的編輯!:) –

+0

JSON.parse(「[object Object]」);你說在json對象中的類型轉換,我可以用它來解析json,但是當我混淆了jsonp,這只是一個js代碼,我需要先將它轉換成json然後我可以解析它。 neverthless我們也可以使用jquery方法window.alert(JSON.stringify(result)); – AshishHashCoder

1

如果你正在向成功回調,這是一個JSONP調用,您val參數已經是一個JavaScript對象。

success: function (result) { 
    console.log(result); 
    console.log(result.someproperty); 
} 

這說明你的錯誤太多,如果result是一個對象,因爲JSON.parse({})拋出了同樣的錯誤。

SyntaxError: Unexpected token o 

如果您正在訪問一個支持JSONP,並有jQuery的可用遠程API,它相當於

JSON.parse("[object Object]"); // now you see where `o` came frome 
1

,很容易跳過中間解析步:

// Assuming you have jQuery available: 
$.ajax('http://example.com/jsonpapi?callback=?', { 
    dataType: 'jsonp', 
    success: function(data) { 
    // No parsing necessary. 
    console.log(data); 
    } 
}); 

這會注入一個指向您的JSONP端點的腳本元素,並且端點通過將json包裝到jQuery爲您自動定義的函數調用中進行響應。 callback是JSONP回調的相對標準命名約定,但請仔細檢查API預期的內容。

如果你沒有可用的jQuery或不想使用它,你可以自己定義一個回調函數,並注入一個腳本元素:

var jsonpCallback = function(data) { 
    console.log(data); 
}; 

var s = document.createElement('script'); 

s.src = 'http://example.com/jsonpapi?callback=jsonpCallback'; 

document.getElementsByTagName('head')[0].appendChild(s); 

這基本上就是jQuery的在爲你做JSONP場景。

+0

看到我的編輯!:)我認爲我讓我的問題更清楚:) –

+0

@DanielGustafsson:我不認爲你發佈的內容非常正確。這不是有效的JSON或JSONP。它看起來像開發工具的文本輸出,也許? –

+0

是的,它來自鉻devtool。和我在ajax文章中取得的成就不是一回事嗎? @Dave Ward? –