I’ve got an ICollection<T> of POCO like this:
public class SearchJsonModel
{
public string label { get; set; }
public string category { get; }
}
In my Razor view, i serialize it as follows:
<script type="text/javascript">
var jsonArray = @Html.Raw(Json.Encode(Model));
</script>
But the output is this:
var jsonArray = [
{"category":"Names","label":"Joe"},
{"category":"Names","label":"John"}
];
Which is causing problems because of the quotes around the properties.
I need to access the properties of each JSON object, so I would expect it to be like this:
var jsonArray = [
{category:"Names",label:"Joe"},
{category:"Names",label:"John"}
];
That way i can do something like this:
$.each(jsonArray, function(index, item) {
var x = item.category;
});
What am i doing wrong? Am i using the wrong method to encode?
This JSON is valid (checked in JSONLINT). The quotes should be there.
You can get the category values like this without any problem
Sample http://jsfiddle.net/DKnBh/