所以我在JSON文件和C#之间转换数据时遇到了很多麻烦。到目前为止,我可以通过Asp.net表单轻松地将数据添加到JSON文件中,这很好,但是它会覆盖那里的所有现有数据,基本上我想在不发生这种情况的情况下添加到文件中。
以下是我想要实现的C#代码。我相信我是如此接近,你可以看到我尝试将JSON文件放入字符串之前添加,然后在通过单独的字符串添加新数据后将它们添加回文件,但它只是不起作用。 / p>
抬头:我是新手,我已经研究了这个网站和谷歌几个小时,如果不是几天试图了解我出错的地方,因为我知道这很容易,但我只是不明白。
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>();
}
}
答案 0 :(得分:0)
我不确定您为什么要保留以前的序列化数据,因为在写入新数据时它应该是“旧的”。但是,如果你看一下the documentation for File.WriteAllText
(enphasis mine):
创建新文件,将指定的字符串写入文件,然后 关闭文件。如果目标文件已存在,则会覆盖。
您有几个选择:
File.WriteAllText
File
方法,例如File.AppendText
:创建一个将UTF-8编码文本附加到现有文本的StreamWriter 文件,如果指定的文件不存在,则为新文件。
但是,如果您希望将所有BookCollection
作为列表保留在文件中,我建议您以List<BookCollection>
的形式写入/读取文件,这样就不会必须考虑处理不同的对象。