Json 源文件代码:
[
{
"Id": "0",
"Name": "书籍",
"Detail": [
{
"ParentName": "书籍",
"Name": "苹果",
"URL": "wwww.baidu.com"
},
{
"ParentName": "书籍",
"Name": "香蕉",
"URL": "wwww.baidu.com"
}
]
},
{
"Id": "1",
"Name": "水果",
"Detail": [
{
"ParentName": "水果",
"Name": "苹果",
"URL": "wwww.sohu.com"
},
{
"ParentName": "水果",
"Name": "香蕉",
"URL": "wwww.sohu.com"
}
]
} ]
C# 读取文件内容:
var jsonPath = Server.MapPath("~/Scripts/Products.json");
string config = File.ReadAllText(jsonPath);
List<ProductInfo> CertConfigs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProductInfo>>(config);
构造类:
public class ProductInfo
{
public string Id { get; set; }
public string Name { get; set; }
public List<ProductDetail> Detail { get; set; }
}
public class ProductDetail
{
public string ParentName { get; set; }
public string Name { get; set; }
public string URL { get; set; }
}
Js 读取源文件代码:
var option = '';
$.getJSON("Scripts/Products.json", function (jsonData) {
$.each(jsonData, function (index, detailInfo) {
option1 += "<option id=" + detailInfo.id + ">" + detailInfo.name + "</option>";
});
$("#jsonProduct").append(option1);
$("#jsonProduct").bind("change", function () {
//选择触发事件
})
});
H5代码
<select id="jsonProduct"></select>