How to serialize List into string using C#?

The non-generic type cannot be used with type arguments 

String jsonstring =JsonSerializer<list<modelgoogle>>(AddressList);

Here modelgoogle is the name of the class in which I have declared the variables and address list is the list.

List<modelgoogle> Addresslist = new list<modelgoogle>(); 

Hope u understand this I don't have any concept of JSON serialization and in my code which I have taken from net is all fine except this line 

String jsonstring =JsonSerializer<list<modelgoogle>>(AddressList)

Asked by:- SanaFarooq
: 252 At:- 8/24/2017 1:08:46 PM
Json Json serilazation

2 Answers
profileImage Answered by:- vikas_jk

 If I am correct, and understanding your question you want to Serialize your List into string using C#

You can use the parser to achieve this

using Newtonsoft.Json;

var json = JsonConvert.SerializeObject(aList);

You may need to install it using NuGet

PM> Install-Package Newtonsoft.Json

Or Second Method, which is old one

using System.Web.Script.Serialization;

var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(aList);

I Hope this helps, let me know if it works by commenting on the answer, thanks

At:- 8/24/2017 2:30:12 PM

profileImage Answered by:- jaya

here is the another possible solution for you question using DataContractJsonSerializer

  1. DataContractJsonSerializer

    var serializer = new DataContractJsonSerializer(TheList.GetType());
    using (var stream = new MemoryStream())
        serializer.WriteObject(stream, TheList);
        using (var sr = new StreamReader(stream))
            return sr.ReadToEnd();

    Note, that this option requires a definition of a data contract for your class:

    public class MyObjectInJson
       public long ObjectID {get;set;}
       public string ObjectInJson {get;set;}
At:- 8/24/2017 2:48:32 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly