I'm working on a file to add/retrieve the data. The data format is JSON. I'm a newbie. I'm using JSON.NET to serialize and deserialize the data. this is the JSON format
{
"EmpId": 1,
"Name": "Kaushik",
"Designation": ".net Developer",
"JoiningDate": "09/23/2013",
"Skill": [
{
"Id": 1,
"SkillName": "C#"
},
{
"Id": 2,
"SkillName": "PHP"
},
{
"Id": 3,
"SkillName": "java"
}
]
}
This is the JSON format I'm working on . Description Of the problem
- I want to append the file data so I can add more JSON to this. For this, do I need to check if there is already some data or not?
- I want to append the data in the JSON file. The appended form of data will look like this
[
{
"EmpId": 1,
"Name": "Kaushik",
"Designation": ".net Developer",
"JoiningDate": "09/23/2013",
"Skill": [
{
"Id": 1,
"SkillName": "C#"
},
{
"Id": 2,
"SkillName": "PHP"
},
{
"Id": 3,
"SkillName": "java"
}
]
},
{
"EmpId": 1,
"Name": "Kaushik",
"Designation": ".net Developer",
"JoiningDate": "09/23/2013",
"Skill": [
{
"Id": 1,
"SkillName": "C#"
},
{
"Id": 2,
"SkillName": "PHP"
},
{
"Id": 3,
"SkillName": "java"
}
]
} ]
My main problem is that I'm not getting the exact logic to append the file .
I'm reading the file char by char as follows:
int count = 0;
EmployeeDetail employee = new EmployeeDetail
{
EmpId = ++count,
Name = formCollection["Name"],
Designation = formCollection["Designation"],
JoiningDate = formCollection["JoiningDate"],
Skill = new List<Skills>
{
new Skills(){Id = 1, SkillName = "C#"},
new Skills(){Id = 2, SkillName = "PHP"},
new Skills(){Id = 3, SkillName = "java"}
}
};
string json = JsonConvert.SerializeObject(employee, Formatting.Indented);
var dataFile = Server.MapPath("~/App_Data/json_data.json");
//Reading the file
StreamReader reader = new StreamReader(dataFile);
int Tchar = 0;
char ch;
do
{
ch = (char)reader.Read();
Response.Write(ch);
Tchar++;
} while (!reader.EndOfStream);
reader.Close();
StreamWriter file = new StreamWriter(dataFile, append: true);
file.WriteLine(json);
file.Close()