问题
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()
回答1:
Your best and most reliable approach would be to not append to the file, but instead, read the entire JSON file and de-serialize to an object, append to the object collection and then serialize the output back to the JSON file.
I created this sample for you (change paths and class accordingly):
var filePath = @"C:\Users\grahamo\Documents\Visual Studio 2013\Projects\WebApplication1\WebApplication1\bin\path.json";
// Read existing json data
var jsonData = System.IO.File.ReadAllText(filePath);
// De-serialize to object or create new list
var employeeList = JsonConvert.DeserializeObject<List<EmployeeDetail>>(jsonData)
?? new List<EmployeeDetail>();
// Add any new employees
employeeList.Add(new EmployeeDetail()
{
Name = "Test Person 1"
});
employeeList.Add(new EmployeeDetail()
{
Name = "Test Person 2"
});
// Update json data string
jsonData = JsonConvert.SerializeObject(employeeList);
System.IO.File.WriteAllText(filePath, jsonData);
As you are new, up-vote or tick as answer if I have helped you out.
回答2:
There is a CopyTo
method on FileStream
.
So you don't need to read char-by-char, open a file stream and CopyTo
to the response stream.
Also I would recommend you to reserialize the file first (using Json.NET, you can simply use dynamic
to avoid messing with concrete types), and since it is an array of objects, then add your new object to this array and save it back.
The format will always be correct since you re-save the whole file.
P.S. You probably will not be able to just append since the serialized JSON object in file is "closed" (you have your } ]
there), so appending text will not work.
来源:https://stackoverflow.com/questions/20626849/how-to-append-a-json-file-without-disturbing-the-formatting