2017-10-05 97 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 起作用。非常感谢你 –

相关问题