更新JSON文件而不是添加

时间:2018-05-15 08:34:46

标签: c# json

我正在尝试更新从我的JSON文件反序列化的信息。该列表以可编辑的asp.net格式填充,我想编辑该项目,然后更新JSON文件。当我进行更改时,它会将新文件添加到列表中,而不是更新原始文件。 如何更新而不是添加。

提前致谢!

public partial class EditBook : System.Web.UI.Page
 {
Catalogue catalogueInstance;

//Filepath for json file
const string FILENAME = 
 @"C:\Users\tstra\Desktop\19456932_CSE2ICX_Assessment_3\Bin\Books.json";


protected void Page_Load(object sender, EventArgs e)
{
    // reading data contained in the json filepath
    string jsonText = File.ReadAllText(FILENAME);

    //convert objects in json file to lists
    catalogueInstance = JsonConvert.DeserializeObject<Catalogue>(jsonText);

    ddlEdit.DataSource = catalogueInstance.books;
    ddlEdit.DataTextField = "title";
    ddlEdit.DataValueField = "id";

    //binding the data to Drop Down List
    ddlEdit.DataBind();
}

    protected void btnSubmit_editBook(object sender, EventArgs e)
  {
    int id = Int32.Parse(txtID.Text);
    string title = txtTitle.Text;
    int year = Int32.Parse(txtYear.Text);        
    string author = txtAuthor.Text;
    string publisher = txtPublisher.Text;
    string isbn = txtISBN.Text;

    catalogueInstance.books.Add(new Book(id, title, author, year, publisher, 
    isbn));

    string jsonText = JsonConvert.SerializeObject(catalogueInstance);
    File.WriteAllText(FILENAME, jsonText);

    txtSummary.Text = "Book ID of " + id + " Has Been Updated in the 
    Catalogue" + Environment.NewLine;
    }

    protected void ddlEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        Book b = catalogueInstance.books[ddlEdit.SelectedIndex];
        txtID.Text = b.id.ToString();
        txtTitle.Text = b.title;
        txtAuthor.Text = b.author;
        txtYear.Text = b.year.ToString();
        txtPublisher.Text = b.publisher;
        txtISBN.Text = b.isbn;
    }
}

3 个答案:

答案 0 :(得分:1)

看起来你每次都要添加一本新书。有了这条线

catalogueInstance.books.Add(new Book(id, title, author, year, publisher,isbn));

看一下你想要编辑的书的实例。

var book = catalogueInstance.books.firstOrDefault(b => b.id == id);

然后更新你的书。

答案 1 :(得分:0)

很明显,您要在btnSubmit_editBook中添加项目,这样就像:

protected void btnSubmit_editBook(object sender, EventArgs e)
 {
    int id = Int32.Parse(txtID.Text); 
    Book book = catalogueInstance.books.SingleOrDefault(x=> x.id == id);
    if(book != null)
    {
       book.title = txtTitle.Text;
       book.year = Int32.Parse(txtYear.Text);        
       book.author = txtAuthor.Text;
       book.publisher = txtPublisher.Text;
       book.isbn = txtISBN.Text;
       txtSummary.Text = "Book ID of " + id + " Has Been Updated in the 
          Catalogue" + Environment.NewLine;
    } 
    string jsonText = JsonConvert.SerializeObject(catalogueInstance);
    File.WriteAllText(FILENAME, jsonText);

}

答案 2 :(得分:0)

嘿尝试替换下面的行

File.WriteAllText(FILENAME, jsonText);

 if (!File.Exists(FILENAME))
                {
                    var myFile = File.Create(FILENAME);
                    myFile.Close();
                }
                 File.WriteAllText(FILENAME, jsonText);

然后,只会反复写入一个文件。