0

I have a problem to serialize a json from a list of object

My goal is to have this format =>

    var tag =                 
    {
            RCP: {name: "Dossier à présenter en RCP", type: "checkbox", events: {change: function(e) { console.log(e.data); console.log(e); } }, callback: function(key, opt){ console.log("key : " + key); console.log(opt); alert(opt.$trigger.attr("id")); }},
            COL: {name: "Dossier à présenter en colloque", type: "checkbox", callback: function(key, opt){  console.log("key : " + key); console.log(opt); alert(opt.$trigge.attr("id"));  }},
            COM: {name: "Commentaire", type: "textarea", callback: function(key, opt){  console.log("key : " + key); console.log(opt); alert(opt.$trigge.attr("id"));  }}
    };

I'm using EF to retrieve the data as this :

        var list = (from e in l_entities.TAG
                    where e.tag_site_code.Trim() == siteCode.Trim()
                    select new CvrTag
                    {
                        Id = e.tag_id,
                        Name = e.tag_libelle,
                        Type = e.tag_site_code
                    }
                ).ToList();

But I retrieve a classic Array when I use JsonConvert.SerializeObject(list).

So my question is : - How to have braces instead array's brackets - How to have an id (ie: RCP or COL) before the json object without quotes - Same to inside json object (ie: name or type)

Thanks for your help

tereško
  • 58,060
  • 25
  • 98
  • 150
User.Anonymous
  • 1,719
  • 1
  • 28
  • 51

1 Answers1

0

Since you are invoking ToList(), your serialization will be a list/array. If you want an object instead, use ToDict():

var dict = (from e in l_entities.TAG
            where e.tag_site_code.Trim() == siteCode.Trim()
            select new CvrTag
            {
                Id = e.tag_id,
                Name = e.tag_libelle,
                Type = e.tag_site_code
            }
        ).ToDict(t => t.Id);
dlebech
  • 1,817
  • 14
  • 27
  • Dictionnary is perfect for my usage, thank you. I need now to remove quote on key :) – User.Anonymous May 31 '13 at 10:09
  • In JavaScript, even if your key is a string (quoted) you can access it with dot notation so removing the quote on the key should not be necessary for most use cases. In other words, this is valid: `var tag = { "RCP": {...} }; alert(tag.RCP);`. – dlebech May 31 '13 at 11:50