I'm using ASP.NET MVC to create a JSON key/value string and Javascript to access it. I've tried a few examples (see below) but only one of them seem to work, where I hardcode the id of the element in (which is obviously not what I want). Everything else throws undefined or an exception. To clarify, I'm just trying to get the value (phrase) from the JSON string, given the key.
My code is as follows.
public string GetNcpJsonDictionary()
{
// Get a list of all dictionary items.
var db = Database.GetDatabase(EnvironmentHelper.IsProduction ? "pub" : "web");
var ncpDictionaryFolder = db.GetItem(ItemIdMapper.Instance.DictionaryNcpItemId);
var ncpDictionaryItems = ncpDictionaryFolder.Axes.GetDescendants().ToList();
var condensedDictionaryItemList = new List<DictionaryItem>();
foreach (var dictionaryItem in ncpDictionaryItems)
{
condensedDictionaryItemList.Add(new DictionaryItem
{
Key = SitecoreApiHelper.GetFieldValue(dictionaryItem.ID.ToString(), "Key"),
Phrase = SitecoreApiHelper.GetFieldValue(dictionaryItem.ID.ToString(), "Phrase")
});
}
var result = JsonConvert.SerializeObject(condensedDictionaryItemList);
return result;
}
In my view, I put:
<script>
window.ncpDictionary = @Html.Action("GetNcpJsonDictionary", "NCP");
</script>
Javascript:
var dict = window.ncpDictionary;
if (dict != null) {
$("label[for='AccountBaseModel_Person_Address']").append(dict['we-cannot-deliver-to-po-boxes']);
}
The JSON outputs like this:
[{"Key":"some-key","Phrase":"some phrase"},...,...]
Debugging the JS shows me this..
But dict['we-cannot-deliver-to-po-boxes'] returns undefined.
I've also tried:
dict.we-cannot-deliver-to-po-boxes
dict.getString('we-cannot-deliver-to-po-boxes')
This will work (but can't be used, obviously):
dict[63]['Phrase']
There is probably an easy fix out there, but I haven't found one.

Keythe other beingPhrase, and you can't access those objects by value ?