因此,我在轉換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