因此,我在转换JSON文件和C#之间的数据时遇到了很多麻烦。到目前为止,我可以通过Asp.net表单很容易地将数据添加到JSON文件中,但它覆盖了现有的任何数据,基本上我想添加到文件中而不会发生这种情况。asp.net c#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](https://msdn.microsoft.com/en-us/library/system.io.file.writealltext(v = vs.110).aspx)的文档吗?也许你应该从那里开始:'创建一个新文件,将内容写入文件,然后关闭文件。如果目标文件已经存在,它将被覆盖。' – Igor
您需要*将json附加到文件的步骤如下:1)获取要添加的内容。 2)将现有内容读回内存3)合并数据结构4)将合并结构写回文件(现在*可以使用'File.WriteAllText')。 – Igor