Serialize JSON dictionary <string, string>

I have the following code, I want the end to be JSON that can read autocomplete jQuery.

[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static string GetNames(string prefixText, int count)
{        
     Trie ArtistTrie = (Trie)HttpContext.Current.Cache["CustomersTrie"];

     List<string> list = ArtistTrie.GetCompletionList(prefixText, 10);
     Dictionary<string, string> dic = new Dictionary<string, string>();
     foreach (string a in list)
     {
         dic.Add(a, "name");
     }
    string json = JsonConvert.SerializeObject(dic, Formatting.Indented);
    return json;

 }

JSON is as follows:

  {
     "the album leaf": "name",
     "the all-american rejects": "name",
     "the allman brothers band": "name",
     "the animals": "name",
     "the antlers": "name",
     "the asteroids galaxy tour": "name",
     "the avett brothers": "name",
     "the band": "name",
     "the beach boys": "name",
     "the beatles": "name"
  }

It's back i want

    "name" : "the allman brothers"

but .... the dictionary needs a unique key, and the same values ​​are in order,

What is an easy fix for this, is it also readable from jQuery?

+3
source share
2 answers

An easy fix for this is to NOT use a dictionary and use a custom data object instead, possibly with one property:

class Album
{
  public string Name{get;set;}
}

Now you can Serialize / Deserialize the list of this custom class.

 string json = JsonConvert.SerializeObject(YourListOfAlbum, Formatting.Indented);   

+2

, "", . , :

[
  {"name": "the allman brothers"},
  {"name": "the beach boys"}
]
+3

All Articles