【发布时间】:2017-10-05 10:25:16
【问题描述】:
所以我在 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 的文档吗?也许你应该从那里开始:
Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten. -
需要将 json 附加到文件的步骤是: 1) 获取要附加的内容。 2) 将现有内容读回内存 3) 合并数据结构 4) 将合并后的结构写回文件(现在您可以使用
File.WriteAllText)。