1

I use a js lib and I need to produce that json result with ASP .Net MVC :

[
  { "" : "--" },
  { "series-1" : "1 series" },
  { "series-3" : "3 series" },
  { "series-5" : "5 series" },
  { "series-6" : "6 series" },
  { "series-7" : "7 series" },
  { "selected" : "series-6" }
]

At this time that code :

var liste = new Dictionary<string, string>();
foreach(var site in sitesList)
{
    liste.Add(site.Id.ToString(), site.RaisonSociale);
}

return Json(liste,
  JsonRequestBehavior.AllowGet);

produce that JSON result :

{"-1":"Tous","93":"name"}

How can I achieve this ?

Regards

5
  • Well, where do those series key-value pairs coming from? It doesn't look like it matches those in your dictionary. Commented Nov 17, 2015 at 11:08
  • What is sitesList? Commented Nov 17, 2015 at 11:10
  • What are the values of Id and RaisonSociale of the elements of sitesList? If the values are the same that you want to be on the json result, convert the list to array and you should get what you want. Commented Nov 17, 2015 at 11:11
  • Yuval Itzchakov and Mr. Wolf : sitesList are issued from the database. Commented Nov 17, 2015 at 11:56
  • Bsa0 it is kind of key / value pair Commented Nov 17, 2015 at 11:56

2 Answers 2

2

use JArray and JObject to produce your Custom Array

    JArray jArray = new JArray();

    foreach (var site in sitesList)
    {
      JObject jObject = new JObject();
      jObject.Add(site.Id.ToString(), site.RaisonSociale);
      jArray.Add(jObject);
    }
    return jArray;
Sign up to request clarification or add additional context in comments.

Comments

1

This is one way to get it:

var liste = new List<Dictionary<string, string>>();
foreach(var site in sitesList)
{
    liste.Add(new Dictionary<string, string> { {site.Id.ToString(), site.RaisonSociale } } );
}

return Json(liste,
  JsonRequestBehavior.AllowGet);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.