码迷,mamicode.com
首页 > Web开发 > 详细

如何读取 Json 格式文件

时间:2016-02-24 17:14:53      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

如何读取 Json 格式文件

标签:

原文地址:http://www.cnblogs.com/hanxingli/p/5213583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!