2010-05-26 53 views
0

我試圖將一個dictionery轉換成一個json對象,以便我可以在我的前端使用它。Dictionary to Json object,「Expected expression」

sortFields = <%= SchrodersHtmlHelper.ToJson(ViewData["SortInfo"])%>; 

但是,我一直在爲此獲取「預期表達式」,而我卻無能爲力。任何人都可以向我解釋我做錯了什麼?

編輯:轉換工作正常,但我仍然在前端得到這個問題,導致瀏覽器抱怨它。

UPDATE

的JSON根據http://jsonlint.com/是有效的,並且由OP爲解決被報告的問題

{ 
    "Name": "Ascending", 
    "ClassDesignation": "Ascending", 
    "ShareType": "Ascending", 
    "Curr": "Ascending", 
    "DateFirstPriced": "Descending", 
    "Identifier": "Ascending", 
    "OneWeakPerf": "Descending", 
    "OneMonthPerf": "Descending", 
    "ThreeMonthPerf": "Descending", 
    "SixMonthPerf": "Descending", 
    "YTDPerf": "Descending", 
    "OneYearPerf": "Descending", 
    "ThreeYearPerf": "Descending", 
    "FourYearPerf": "Descending", 
    "FiveYearPerf": "Descending", 
    "TenYearPerf": "Descending", 
    "SinceLaunchPerf": "Descending", 
    "OneYearAnnualisedPerf": "Descending", 
    "ThreeYearAnnualisedPerf": "Descending", 
    "FiveYearAnnualisedPerf": "Descending", 
    "TenYearAnnualisedPerf": "Descending", 
    "SinceLaunchAnnualisedPerf": "Descending" 
} 
+3

生成的JSON的外觀如何? – 2010-05-26 16:16:47

+1

做一個「查看源代碼」,然後你可以看到瀏覽器得到了什麼。 – Pointy 2010-05-26 16:18:22

回答

1

這意味着JSON輸出是沒有很好地形成,原因可能是因爲原始格式不正確或者輸出不正確(或根據Pointy的評論)。請參閱this answer以獲取常見錯誤列表。既然你使用的是Javascript本身,而不是JSON,其中一些不適用於你,但有些會。我的猜測是一個懸而未決的逗號;我們需要了解JSON。

您可以使用this validator驗證JSON,並在the JSON site上了解更多所需的語法。

+0

這可能是因爲他的構造沒有任何結果,所以得到的Javascript只是'sortFields =;'。 – Pointy 2010-05-26 16:20:13

+0

@Pointy:的確,或者是一個沒有引號的字符串。 – 2010-05-26 16:20:46

+0

jsonlint.com驗證了他的JSON罰款,順便說一句 – 2010-05-26 16:39:33