Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I want to call json file and write some data on that and again i have to save that json file in C#.Please help me.

What I have tried:

I didnt tried any things.I didnt found any good tutorial related to my question.
Posted
Updated 3-Mar-16 20:07pm
Comments
BillWoodruff 4-Mar-16 1:56am    
There are many excellent introductory-level explanations of what JSON is, and how to use it here on CodeProject, and, of course, on the web. You have clearly made no effort to help yourself; come back after you have "tried something," with specific questions.
Afzaal Ahmad Zeeshan 4-Mar-16 2:30am    
Google for "JSON helper in C#", it may recommend you to use Json.NET; which is a library to work with JSON data in C# applications.

Try with below code:

Create a custom class for type of data serialization:
C#
class employee
{
	public int id { get; set; }
	public string name { get; set; }
}

Create an employee object and serialize it to store in .json file. Add reference System.Web.Extensions and use namespace System.Web.Script.Serialization :
C#
employee eObj = new employee()
{
	id = 90,
	name = "manas"
};

string json = new JavaScriptSerializer().Serialize(eObj);

//write string to file
System.IO.File.WriteAllText(@"C:\path.json", json);
 
Share this answer
 
v2
Json file is plain text. You can make use if static methods in File class read/write data to it. You may choose Stream if files are huge.

Note that with these methods, you will need to ensure that you are using correct JSON format.

Alternatively,
-> you can read the JSON, deserialize it into an object. 
-> Create more objects of same class or change data in existing object.
-> Serialize objects to json
-> Overwrite/append the file as per your need
 
Share this answer
 
Comments
BillWoodruff 4-Mar-16 1:55am    
This is not a solution; it's advice. And, there really is no "question" here: the OP clearly hasn't made any effort.
dan!sh 4-Mar-16 2:00am    
You just summarized 89.334% of Q & A. :)

I don't want to give out code. These pointers should help OP to write something by themselves.
Member 14111149 12-Mar-19 3:03am    
if we want to add more than one information into json how you do it ??

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