Json Source file Code:
[
{
"Id": "0",
"Name": "Books",
"Detail": [
{
"ParentName": "Books",
"Name": "Apple",
"URL": "Wwww.baidu.com"
},
{
"ParentName": "Books",
"Name": "Banana",
"URL": "Wwww.baidu.com"
}
]
},
{
"Id": "1",
"Name": "Fruit",
"Detail": [
{
"ParentName": "Fruit",
"Name": "Apple",
"URL": "Wwww.sohu.com"
},
{
"ParentName": "Fruit",
"Name": "Banana",
"URL": "Wwww.sohu.com"
}
]
} ]
C # reads the file contents:
var jsonPath = Server.MapPath ("~/scripts/products.json");
string config = File.readalltext (jsonPath);
list<productinfo> Certconfigs = newtonsoft.json.jsonconvert.deserializeobject<list<productinfo> > (config);
Construct class:
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 Read source file code:
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 () {
Select Trigger Event
})
});
H5 Code
<select id= "Jsonproduct" ></select>
How to read Json format files