所以我在 JSON 文件和 C# 之间转换数据时遇到了很多麻烦。到目前为止,我可以通过 Asp.net 表单轻松地将数据添加到 JSON 文件,这很棒,但它会覆盖那里的任何现有数据,基本上我想添加到文件而不发生这种情况。
下面是我想要实现的 C# 代码。我相信我已经很接近了,正如你所看到的,我尝试在添加之前将 JSON 文件放入一个字符串中,然后在通过单独的字符串添加新数据后将它们都添加回文件中,但它不起作用。
注意:我是新手,我在这个网站和谷歌上研究了几个小时,甚至几天,试图了解我哪里出错了,因为我知道这非常简单,但我就是不明白。
public partial class AddBook : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
const string FILENAME = "Books.json";
string fullFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME);
string jsonText2 = File.ReadAllText(fullFileName);
BookCollection bookCollection2 = JsonConvert.DeserializeObject<BookCollection>(jsonText2);
if (!Page.IsPostBack)
{
if (Session["EditDetails"] != null && (Boolean)Session["editDetails"] == true)
{
BookYear(DDLYear);
}
else
{
BookYear(DDLYear);
}
}
else if(IsPostBack) {
String BookID = TxtBookID.Text;
String Title = TxtTitle.Text;
String Author = TxtAuthor.Text;
String Year = DDLYear.Text;
String Publisher = TxtPublisher.Text;
String ISBN = TxtISBN.Text;
BookCollection bookCollection = new BookCollection();
Book book = new Book(BookID, Title, Author, Year, Publisher, ISBN);
bookCollection.books.Add(book);
string jsonText = JsonConvert.SerializeObject(bookCollection);
File.WriteAllText(fullFileName, jsonText);
jsonText2 = JsonConvert.SerializeObject(bookCollection);
}
}
protected void BookYear(DropDownList dropDownList)
{
int yr = DateTime.Now.Year;
dropDownList.Items.Insert(0, "Select Year");
for (int x = 1900; x < yr; x++)
{
dropDownList.Items.Add(x.ToString());
}
}
protected void BtnSubmit_Click(object sender, EventArgs e)
{
Response.Redirect(Request.RawUrl);
//String BookID = TxtBookID.Text;
//String Title = TxtTitle.Text;
//String Author = TxtAuthor.Text;
//String Year = DDLYear.Text;
//String Publisher = TxtPublisher.Text;
//String ISBN = TxtISBN.Text;
//const string FILENAME = "Books.json";
//string fullFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME);
//BookCollection bookCollection = new BookCollection();
//Book book = new Book(BookID, Title, Author, Year, Publisher, ISBN);
//bookCollection.books.Add(book);
//string jsonText = JsonConvert.SerializeObject(bookCollection);
//File.WriteAllText(fullFileName, jsonText);
}
}
我也有单独的课程
public class Book
{
public String id { get; set; }
public string title { get; set; }
public string author { get; set; }
public String year { get; set; }
public string publisher { get; set; }
public String isbn { get; set; }
public Book(String id, string title, string author, String year, string
publisher, String isbn)
{
this.id = id;
this.title = title;
this.author = author;
this.year = year;
this.publisher = publisher;
this.isbn = isbn;
}
和
public class BookCollection
{
public List<Book> books { get; set; }
public BookCollection()
{
this.books = new List<Book>();
}
}
我不确定为什么你想要保留以前的序列化数据,因为当写入新数据时它应该是“旧的”。但是,如果您查看
File.WriteAllText
的文档(重点是我的):
创建一个新文件,将指定的字符串写入该文件,然后 关闭文件。如果目标文件已经存在,它会被覆盖。
您有几个选择:
File.WriteAllText
File
方法,例如 File.AppendText
:创建一个 StreamWriter,将 UTF-8 编码文本附加到现有的 文件,或者如果指定的文件不存在则到一个新文件。
但是,如果您想将所有
BookCollection
作为文件中的列表保留,我建议您将文件作为 List<BookCollection>
写入/读取,这样您就不必考虑处理不同的物体。
您可以使用“File.AppendAllText”方法,链接如下: 此方法不会覆盖 JSON 文件。
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.appendalltext?view=net-8.0