1

json データがあり、それを 2 セットのデータ配列に分割しようとしています。1 つの json データは「構築」データ値からのすべての値を保持し、2 番目の json データは残りの配列と製品、システムの値を保持します。などなど

getJSON に渡す前に、2 つの配列に分割したいと考えています。それらを2つに分割したら、残りのJSONデータをgetJSONに渡します。逆も同様です

getJSON に渡す前に、それらを 2 つの配列に分割するにはどうすればよいですか? 私の現在のコード

    <script type="text/javascript">

    $(document).ready(function () // don't do anything until the document is loaded.
   {

           var baseUrl = "config.json";

           console.log(baseUrl.html.splice(3)); // remove and log the empty fieldset
                var constructgrp = baseUrl.html.shift(); // remove the first item (caption: "Construct")

                var restofJSONdata = baseUrl.html; 
               alert(constructgrp);


           $.getJSON(restofJSONdata, function (data) // call getJSON providing the complete url with search term and a JSONP callback
           {
               // console.log(data.html.splice(2,3));
              // console.log("data " +data);
              /*
               console.log(data.html.splice(3,0)); // remove and log the empty fieldset
                var constructgrp = data.html.shift(); // remove the first item (caption: "Construct")

                var restofJSONdata = data.html; 
               alert(constructgrp); :*/

               $("#demo-3-form").empty(); // clear out any previous results.
               if (data.html.length < 1) $('#demo-3-forms').html("No results. Nada. Nuttin. Zippo.");

               //$("#demo-3-form").empty(); // clear out any previous results.
              // if (data.html.length < 1) $('#demo-3-forms').html("No results. Nada. Nuttin. Zippo.");
            //   $.each(this.constructgrp, function () // iterate over the results, constructing the HTML for the display.
              // {
                /*
                     var html =constructgrp.type +' :';
                     html += '<b>'+constructgrp.caption +'</b><br>';

                     html += ' <br>';
                       $.each(constructgrp.html, function() {
                         //  alert(this.name);
                          html +='Title :' +this.title +'<br>';
                           html +='Name :' +this.name +'<br>';
                           html +='Value :' +this.value +'<br> ';
                           html +='Caption :' +this.caption +'<br><br> ';
                          // html += this.type +'<br><br> ';
                       });  
                   $('#demo-3-form').hide().append(html).fadeIn(800); // fade in the results over 2 seconds.
                   */
              // });



                $.each(restofJSONdata, function () // iterate over the results, constructing the HTML for the display.
               {

                     var html =this.type +' :';
                     html += '<b>'+this.caption +'</b><br>';

                     html += ' <br>';
                       $.each(this.html, function() {
                         //  alert(this.name);
                          html +='Title :' +this.title +'<br>';
                           html +='Name :' +this.name +'<br>';
                           html +='Value :' +this.value +'<br> ';
                           html +='Caption :' +this.caption +'<br><br> ';
                          // html += this.type +'<br><br> ';
                       });  
                   $('#demo-3-form').hide().append(html).fadeIn(800); // fade in the results over 2 seconds.
               });


           });
       //});
   });




    </script>

JSON コード config.json

{
    "html": [{
        "type": "fieldset",
        "caption": "Construct",
        "html": [{
            "title": "tooltip data for rk",
            "name": "rk_",
            "value": "24",
            "caption": "DNS Servers (a list of IP addresses separated by comas)",
            "type": "textarea"
        }, {
            "title": "tooltip data for rk_ntpservers",
            "name": "rk_ntpservers",
            "value": "111.11.11.11",
            "caption": " separated by comas",
            "type": "textarea"
        }, {
            "title": "tooltip data for ff_eth0_ff",
            "name": "ff_eth0_ff",
            "value": "18",
            "caption": "Public  Address (0.0.0.0 to disable)",
            "type": "text"
        }, {
            "title": "tooltip data for ff_eth0_netmask",
            "name": "ff_eth0_netmask",
            "value": "2.2.2.0",
            "caption": "Public rk Netmask",
            "type": "text"
        }, {
            "title": "tooltip data for ff_eth1_ff",
            "name": "ff_eth1_ff",
            "value": "0.0.0.0",
            "caption": "MG Device rk (0.0.0.0 to disable)",
            "type": "text"
        }, {
            "title": "tooltip data for ff_eth1_netmask",
            "name": "ff_eth1_netmask",
            "value": "2.2.2.0",
            "caption": "MG Device rk Netmask",
            "type": "text"
        }, {
            "title": "tooltip data for ff",
            "name": "ff_gateway",
            "value": "1",
            "caption": "Gateway",
            "type": "text"
        }]
    }, {
        "type": "fieldset",
        "caption": "Product",
        "html": [{
            "title": "tooltip data for product_ident",
            "name": "product_ident",
            "value": "78",
            "caption": "Product Name",
            "type": "text",
            "disabled": "disabled"
        }, {
            "title": "tooltip data for product_svnversion",
            "name": "product_svnversion",
            "value": "7916",
            "caption": "Revision",
            "type": "text",
            "disabled": "disabled"
        }]
    }, {
        "type": "fieldset",
        "caption": "System ",
        "html": [{
            "title": "tooltip data for system_license",
            "name": "system_license",
            "value": "HH",
            "caption": "License",
            "type": "text"
        }, {
            "title": "tooltip data for system_variant",
            "name": "system_variant",
            "value": "normal",
            "caption": "Variant",
            "type": "text"
        }]
    }, {
        "type": "fieldset",
        "class": "btn-fieldset",
        "caption": "",
        "html": [{
            "type": "submit",
            "id": "submitbtn",
            "class": "btn btn-primary",
            "value": "Save"
        }]
    }]
}
4

1 に答える 1

0

getJSON に渡す前にそれらを 2 つの配列に分割するには、次のことを行う必要があります。

  1. JSON を取得する
  2. データを 2 つに分割する

    var construct;
    
    $.getJSON('PATH_TO_config.json', function(data) {
    
        // splice will return first array and leaves rest in original data
        construct = data.html.splice(0, 1);
    
        // in the first parameter you have data.html[0] in the second
        // data.html[1..3] from the original JSON
        doSomethingWithData(construct, data.html);
    }
    
于 2013-01-29T22:49:52.390 に答える