我特灵使用JSON文件作为ASP.net(一个图片库)中静态内容的数据库。我可以读取文件,检索数据,并以一种简单的方式显示它们。我目前使用的是JSON.net和c# serverside。
这是我的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");
}
}
我怎样才能得到它?
先谢了!
2条答案
按热度按时间9o685dep1#
4bbkushb2#
这是个老问题了,但是如果你想用一个文件作为一个json数据库,你可以选择this link,它是一个专业且易于使用的库