views:

51

answers:

2

I'm trying to convert a dictionery into a json object, so that I can work with it in my front end.

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

However, I keep on getting "Expected expression" for this all the time and I'm clueless why. Could anyone explain to me what I'm doing wrong?

Edit: The conversion works fine, but I still get this issue in the front end, causing the browser to complain about it.

UPDATE

The JSON is valid according to http://jsonlint.com/, and the issue is reported as resolved by the 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"
}
+1  A: 

It means that the JSON in the output isn't well-formed, either because the original is malformed or because it's not being output correctly (or at all, per Pointy's comment). See this answer for a list of common errors. Since you're using Javascript itself, not JSON, some of them won't apply to you, but some will. My guess is a dangling comma; we need to see the JSON to know.

You can validate JSON with this validator, and learn more of the required syntax on the JSON site.

T.J. Crowder
It could be that his construct there is resulting in nothing at all, so that the resulting Javascript is just `sortFields = ;`.
Pointy
@Pointy: Indeed, or an unquoted string.
T.J. Crowder
jsonlint.com validated his json fine, btw
Dan Heberden
A: 

{"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"}

This is the way the JSON looks like, and I can't see any issue with it. But I'm not too familiar with JSON either, so don't know for sure.

MrW
Have you verified that it looks exactly like that when you do a "view source" on your page?
Pointy
Also, when adding information to a question, you shouldn't provide an "answer" like this, because obviously it's not an answer to your original question. Just edit your question (click the little "edit" link underneath it).
Pointy
Cool. Sorry about that. Viewing the source doesn't seem to give my much at all.
MrW
Well that right there may be your problem :-) You should see, in the source, `sortFields = {"Name":"Ascending", ... };` and if you don't then something's wrong with that server-side expression.
Pointy
Might this be due it's all in a partial view that causes it not to be displayed in the source? As it seems I can't see any of the information from that page.
MrW
If I'm using FireBug, I can see the object listed in the items under the DOM tab. with the expected values as well.
MrW
The *DOM* tab? Do you mean the "HTML" tab? In the HTML tab, when you find your `<script>` element and open it up, do you see the code there?
Pointy
Yea I know. But the global variable is available in the DOM tab as well. Anyway. Don't know why. I've rolled back my code done back and forward and now suddenly works. When I compare the previous version with this, they are the same. I'm sorry for taking your time. It works no at least!
MrW