我想在ASP.net(图片库)中使用JSON文件作为静态内容的数据库。 一切运作良好:我可以读取文件并检索数据并以简单的方式显示它们。 我目前正在使用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");
}
}
我怎样才能获得它?
提前致谢!
答案 0 :(得分:0)
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);
}
答案 1 :(得分:0)
一个老问题,但如果您想将文件用作 json 数据库,您可以检查 this link。这是一个专业且易于使用的库