JSON 파일과 C#간에 데이터를 변환하는 데 많은 문제가 있습니다. 지금까지 나는 Asp.net 양식을 통해 JSON 파일에 데이터를 쉽게 추가 할 수 있지만 거기에있는 기존 데이터를 덮어 쓰고 기본적으로이 문제없이 파일에 추가하려고합니다.asp.net C# to json 기존 데이터를 덮어 쓰지 않고 데이터를 저장하는 방법
다음은 달성하고자하는 C# 코드입니다. 내가 너무 가깝다고 생각하고 JSON 파일을 추가하기 전에 JSON 파일을 넣은 다음 새로운 데이터가 별도의 문자열을 통해 추가 된 후에 다시 파일에 추가하려고 시도하지만 실제로 작동하지 않습니다.
머리를 위로 : 나는 이것에 초보자 다. 나는 쉽지 않은 it 's를 알고 있기 때문에 내가 잘못 가고있는 것을 이해하려고 노력하고있는 날이 아니라면이 사이트와 Google를 몇 시간 동안 조사했다. 그러나 나는 단지 그것을 얻지 않는다.
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