zoukankan      html  css  js  c++  java
  • 如何读取 Json 格式文件

    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>

     

  • 相关阅读:
    基于百度ueditor的富文本编辑器
    IE 跨域后COOKIE无法更新
    C笔记
    KOHANA3.3 ORM中文详解
    ReflectionClass getDocComment 返回false
    C中的栈区&堆区
    通过GMAP得到坐标对应地址
    准备学习Flex,记录下开始时间。
    法律援助网站!http://www.qinquan.org
    博客园也有刷新提交的问题?
  • 原文地址:https://www.cnblogs.com/hanxingli/p/5213583.html
Copyright © 2011-2022 走看看