2010-05-26 84 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