JSON文件作为数据库
本文关键字:数据库 文件 JSON | 更新日期: 2023-09-27 18:03:07
我试图使用JSON文件作为ASP.net(图像库)中静态内容的数据库。一切都很好:我可以读取文件和检索数据,并以一种简单的方式显示它们。我目前正在使用JSON.net和c#服务器端。
这是我的JSON(手工制作)的一部分:
{
"Category 1": [
{
"TitleFile": "Title of IMG1",
"FileName": "IMG1",
"URLThumbFile": "Content/images/Gallery/IMG1_tb.png",
"URLFile": "Content/images/Gallery/IMG1.png",
"DescFile": "DESC IMG1"
},
{
"TitleFile": "Title of IMG2",
"FileName": "IMG2",
"URLThumbFile": "Content/images/Gallery/IMG2_tb.png",
"URLFile": "Content/images/Gallery/IMG2.png",
"DescFile": "DESC IMG2"
}
],
"Category 2": [
{
"TitleFile": "Title of IMG3",
"FileName": "IMG3",
"URLThumbFile": "Content/images/Gallery/IMG3_tb.png",
"URLFile": "Content/images/Gallery/IMG3.png",
"DescFile": "DESC IMG3"
},
{
"TitleFile": "Title of IMG4",
"FileName": "IMG4",
"URLThumbFile": "Content/images/Gallery/IMG4_tb.png",
"URLFile": "Content/images/Gallery/IMG4.png",
"DescFile": "DESC IMG4"
}
],
"Category 3": [
{
"TitleFile": "Title of IMG5",
"FileName": "IMG5",
"URLThumbFile": "Content/images/Gallery/IMG5_tb.png",
"URLFile": "Content/images/Gallery/IMG5.png",
"DescFile": "DESC IMG5"
}
]
}
现在,我正在考虑如何创建一种"上传表单",以便编写这个JSON文件并存储信息。客户端,一切正常:
<asp:TextBox runat="server" ID="txtFileName" />
<asp:TextBox runat="server" ID="txtFileTitle" />
<asp:TextBox runat="server" ID="txtFileDesc" />
<asp:FileUpload runat="server" ID="UPLFileImg" />
<asp:DropDownList runat="server" ID="ddlCategory"/>
<asp:Button runat="server" ID="btnUploadFile" Text="Upload" OnClick="btnUploadFile_Click" />
c#: //Classes
public class JSONGalleryCategory //is it correct??
{
public List<JSONGalleryContent> Category { get; set; }
}
public class JSONGalleryContent //??
{
public string FileTitle { get; set; }
public string FileName { get; set; }
public string URLThumbFile { get; set; }
public string URLFile { get; set; }
public string DescFile { get; set; }
public DateTime DataFile { get; set; }
}
//in the .aspx.cs
private void PopulateDDL()
{
List<string> items = new List<string>();
dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));
foreach (JProperty category in jsonFile)
{
items.Add(category.Name);
}
ddlCategory.DataSource = items;
ddlCategory.DataBind();
}
protected void btnUploadFile_Click(object sender, EventArgs e)
{
if (UPLFileImg.HasFile)
{
string fileName = Path.Combine(Server.MapPath("~/Content/images/Gallery/"), UPLFileImg.FileName);
UPLFileImg.SaveAs(fileName);
//Open Jsonfile...read content...deserialize?
dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));
//Create new node?
//???
//append new node?
//serialize again and save...? ordering nodes??
Utilities.WriteFile("~/Content/images/Gallery/Gallery.json", JsonConvert.SerializeObject("???", Formatting.Indented));
Utilities.ShowMessage("Success");
}
else
{
Utilities.ShowMessage("Error");
}
}
如何获取?
提前感谢!
一个老问题,但如果你想使用一个文件作为json数据库,你可以检查这个链接。这是一个专业且易于使用的库
dynamic jsonFile = JObject.Parse(Utilities.ReadFile("~/Content/images/Gallery/Gallery.json"));
using (FileStream fs = File.Open(@"~/Content/images/Gallery/Gallery.json", FileMode.Open)) //may have to server.mappath this
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, jsonFile);
}