-1

I am new to C# and am trying to create something like this using c#. How we can do that any suggestion. I just want to create list of dict.

a = [ { "server": "10.14.13.1", "prefer": "Yes" }, { "server": "10.1.2.1", "prefer": "No" } ]

sonali
  • 219
  • 2
  • 11
  • Where this is JSON output? What's C# structure that contains this data that you're trying to output? You would typically have your controller return an object and serialize that as JSON, not try to generate JSON from a Razor file - unless this is to be written into an HTML page in a script tag? – Rup May 14 '20 at 10:23
  • @Rup need in c# to create list of dict. how to do that. – sonali May 14 '20 at 10:32
  • A list of C# dictionaries? Or objects that will serialize into JSON dictionaries? If it's just JSON dictionaries then I'd suggest using a simple class like David's answer or a value tuple. – Rup May 14 '20 at 10:40
  • But you can create a C# dictionary using `new Dictionary`, or using LINQ's `.ToDictionary`, and then just add those to a list. I don't know of an easy way to create a dictionary from constant values though. Or if you're transforming some other list of dictionaries then that's easy to do with LINQ Select etc. too. – Rup May 14 '20 at 10:42
  • @Rup this way i do in python: lst = [] for i in some_source: dict = {} dict["server"] = i.server_ip dict["prefer = i.prefer_type lst.append(dict) – sonali May 14 '20 at 10:49
  • @Rup where server and prefer values will come from some_souce(consider excel) so i will loop and create dict and append into list so at end of loop my list will have multiple dictionary(based on some_source ) so how can I do it in c#? – sonali May 14 '20 at 10:50

1 Answers1

1
public class POCO
{
    [JsonProperty("server")]
    public string Server { get; set; }

    [JsonProperty("prefer")]
    public string Prefer { get; set; }
}

POCO o1 = new POCO("10.1.2.1", "No");
POCO o2 = new POCO("10.1.2.2", "YES");
List<POCO> list = new List<POCO>() {o1, o2};

JsonConvert.SerializeObject(list);  //<-- this is what you want

Notes

  1. this is code snippet only, and we need constructor(a,b) to make it fully working.
  2. You don't have to create the POCO object yourself, there is plenty of online tools, e.g. https://app.quicktype.io/ to help you in this.

Refer to this link also.

enter image description here

David
  • 15,894
  • 22
  • 55
  • 66
  • @David but this is static. I need to create list of dict that dict value comes from another list dict. – sonali May 14 '20 at 10:31
  • @sonali don't quite follow. What do you mean by static and how to create a dict out of another one? – David May 14 '20 at 10:33
  • this way i do in python: lst = [] for i in some_source: dict = {} dict["server"] = i.server_ip dict["prefer = i.prefer_type lst.append(dict) – sonali May 14 '20 at 10:52
  • where server and prefer values will come from some_souce(consider excel) so i will loop and create dict and append into list so at end of loop my list will have multiple dictionary(based on some_source ) so how can I do it in c#? – sonali May 14 '20 at 10:53
  • you can use dynamic object, but you lose the strong type of C#! You have your choice, :) ! – David May 14 '20 at 10:54
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects – David May 14 '20 at 10:55