2017-10-05 90 views
0

因此,我在轉換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>(); 
} 


} 
+0

你看過[File.WriteAllText](https://msdn.microsoft.com/en-us/library/system.io.file.writealltext(v = vs.110).aspx)的文檔嗎?也許你應該從那裏開始:'創建一個新文件,將內容寫入文件,然後關閉文件。如果目標文件已經存在,它將被覆蓋。' – Igor

+0

您需要*將json附加到文件的步驟如下:1)獲取要添加的內容。 2)將現有內容讀回內存3)合併數據結構4)將合併結構寫回文件(現在*可以使用'File.WriteAllText')。 – Igor

回答

0

我不知道你爲什麼會想保留以前的序列化的數據,因爲它應該是「老」的時候,新的是寫。但是,如果你看一下the documentation for File.WriteAllText(enphasis礦):

創建一個新文件,指定的字符串寫入文件,然後 關閉文件。如果目標文件已存在,則將被覆蓋

你有兩個選擇:

  1. 讀取當前的文本和使用File.WriteAllText
  2. 使用其他File方法,如File.AppendText寫了一切:

創建StreamWriter,將UTF-8編碼的文本附加到現有的 文件或附加到新文件cified文件不存在。

不過,如果你想保持所有BookCollection上的文件列表,我建議你讀/寫文件作爲List<BookCollection>,這樣你就不必去想處理不同的對象。

+1

AppendText在下一次反序列化時不會產生可用對象 –

+0

@KyleBurns我很清楚這一點,這就是我答案的最後部分的原因。 –

+0

file.appendtext ...多麼美麗。現在它添加到文件而不擦除。謝謝。我相信你的答案的最後一部分是指現在在JSON文件中它會每次創建一本不允許的新書。我看到了問題,但我缺乏真正修復它的技能。我可以看到你也給出了這個問題的答案,但我真的不明白。不過,我會玩這個代碼,直到這個List 起作用。非常感謝你 –

相關問題