Serialize List> as JSON

后端 未结 2 372
星月不相逢
星月不相逢 2020-12-10 10:38

I\'m very new with JSON, please help!

I am trying to serialise a List> as JSON

Currently:



        
2条回答
  •  不知归路
    2020-12-10 11:05

    You can use Newtonsoft and dictionary:

        var dict = new Dictionary();
        dict.Add(1, "one");
        dict.Add(2, "two");
    
        var output = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
    

    The output is :

    {"1":"one","2":"two"}
    

    Edit

    Thanks to @Sergey Berezovskiy for the information.

    You are currently using Newtonsoft, so just change your List> to Dictionary and use the serialize and deserialize method from the package.

提交回复
热议问题