2012-02-03 57 views
1

我有下面的JSONP完整字符串,從我的aspx頁面返回給客戶端。當字符串返回到JSONP時獲取無效的JSON錯誤Jquery調用

"processjsonp({\"result\": [\"CreateCityKeys\", \"Keyword -Spokane already exists in City\r\nKeyword -Anchorage already exists in City\r\nKeyword -Fairbanks already exists in City\r\nKeyword -Bellingham already exists in City\r\nKeyword -Juneau already exists in City\r\nKeyword -Boise already exists in City\r\nKeyword -Victoria already exists in City\r\nKeyword -Kelowna already exists in City\r\nKeyword -Eugene already exists in City\r\nKeyword -Medford already exists in City\r\nKeyword -Tucson already exists in City\r\nKeyword -Walla Walla already exists in City\r\nKeyword -Wenatchee already exists in City\r\nKeyword -Pasco already exists in City\r\nKeyword -Pullman already exists in City\r\nKeyword -Redmond already exists in City\r\nKeyword -Yakima already exists in City\r\nKeyword -Ketchikan already exists in City\r\nKeyword -Kauai Island already exists in City\r\nKeyword -Kona already exists in City\r\nKeyword -Kahului already exists in City\r\n\"]})" 

我知道錯誤必須是\ r \ n之間,請建議擺脫這一點。

謝謝

M.S.

編輯:錯誤 - JSONP解析錯誤

+0

請說明錯誤。 – 2012-02-03 11:22:48

+0

它只是一個解析器錯誤,當它回到客戶端 – 2012-02-03 11:27:09

回答

1

你的JSON的語法不正確,你可以替換\「只有」和再檢查一下解析器串限制here talks這個

+0

好吧然後我怎麼能顯示所有上面的文本在新行中,因爲這個字符串是從使用StringBuilder.AppendLine,請認真建議 – 2012-02-03 11:34:27

+0

我覺得你可以拆分最長的字符串,然後使用foreach來打印它 – 2012-02-03 11:37:44

+0

你能清楚你想說什麼嗎? – 2012-02-03 11:48:19

0

例如服務器響應:

json = {"result": ["CreateCityKeys", "Keyword -Spokane already exists in City", "Keyword -Anchorage already exists in City", "Keyword -Fairbanks already exists in City", "Keyword -Bellingham already exists in City", "Keyword -Juneau already exists in City", "Keyword -Boise already exists in City", "Keyword -Victoria already exists in City", "Keyword -Kelowna already exists in City", "Keyword -Eugene already exists in City", "Keyword -Medford already exists in City", "Keyword -Tucson already exists in City", "Keyword -Walla Walla already exists in City", "Keyword -Wenatchee already exists in City", "Keyword -Pasco already exists in City", "Keyword -Pullman already exists in City", "Keyword -Redmond already exists in City", "Keyword -Yakima already exists in City", "Keyword -Ketchikan already exists in City", "Keyword -Kauai Island already exists in City", "Keyword -Kona already exists in City", "Keyword -Kahului already exists in City",]} 

演示使用jQuery:

x = jQuery.parseJSON(json) 
$.each(x.result, function(i, item){ 
    document.write('<p>' + item + '</p>'); 
}); 
+0

Hwy Toloo建議,我不想打印所有字符串只有結果[1],因爲我不想打印「CreateCityKeys」,並且在動態創建上面的格式字符串時不會更容易,請使用另一種方式 – 2012-02-04 06:12:49

+0

告訴我們你真正想要的是什麼,僅僅是第二項(意味着結果[1])只需替換$。 ... with document.write(x.result [1]);這可以嗎? – 2012-02-06 09:33:47

相關問題