Click here to Skip to main content
15,887,849 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I am trying to write a list<> output in a text file in JSON format.
Con somebody suggest me the way to do so.

Thanks in Advance.
Posted
Comments
gggustafson 20-Aug-14 1:14am    
By "JSON format" do you mean "Javascript Object format"?
Bhupendra Chauhan 20-Aug-14 1:38am    
Yes, JSON stands for Java Script Object Notation.

You Can construct the JSON object for the list by using
C#
System.Web.Script.Serialization.JavaScriptSerializer jSearializer = 
                   new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonData = jSearializer.Serialize(userList);//userList means your list<> object

Now you can write jsonData to the file using
System.IO.File.AppendAllText(@"C:\Output.txt",jsonData);// You can use your own file path here
 
Share this answer
 
v3
C#
List<object> mylist;

string json = mySerializerOfChoiceToJSON(mylist); // replace this with the serializer you want
File.WriteAllText("filename.ext", json); // set your file name here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900