如何从输入生成嵌套的Json对象?

时间:2019-05-01 09:30:04

标签: javascript jquery html json

我需要白天根据输入生成以下Json对象

{
  "name":"USA",
  "parents": [
     {
      "state":"California",
      "id":"12",
      "child":[
          {
             "city":"Los Angeles",
             "id":"1"
          },
          {
             "city":"San Francisco",
             "id":"2"
          }
       ]  
      },
      {
       "state":"Texas",
       "id":"33",
       "child":[
           {
              "city":"Dallas",
              "id":"3"
           },
           {
              "city":"Houston",
              "id":"4"
           }
        ]  
    }
  ] 
}

此输入表中的

<table id="tTypeTable" class="table table-bordered" hidden="hidden" >
     <tr>
        <th><a class="btn btn-primary" onclick="add_tTypeParentField();"></th>
     </tr>
  <!-- ko foreach: {data: tTypeParentFields, as: 'tParentField'} -->
   <tr>
          <td>
           <table class="table table-hover" >
            <tr>
              <th> State </th>
              <th> </th>
            </tr>
            <tr title="tParentRow" >
              <td>
                <input id="state" data-bind="value: tParentField[0]" 
                  onblur="createJSON()"/>
              </td>                                          
              <td>
              <input id="value"  data-bind="value: tParentField[0]" 
                    onblur="createJSON()"/>
              </td>                                           
            </tr>
           </table>
        <table id="tChild" class="table table-hover"  >
           <tr>
             <th> City </th>
             <th> </th>
             <th> <a id="childAdd" onclick="addChild(this);"></th>
           </tr>
         <tbody>
          <tr title="tChildRow" id="tChildRow">
            <td>
             <input id="cityName"  onblur="createJSON()"/>
            </td>
            <td>
             <input  id="cityValue" " onblur="createJSON()"/>
            </td>
            <td>
             <a id="removeChild" onclick="deleteChild(this)" ></a>
            </td>
          </tr>
         </tbody>
        </table>
       <td> <a  data-bind="click: removeTTypeParentField" ></a></td>
     </td>
   </tr>
    <!-- /ko -->
</table>
 <div class="col-md-11 ">
   <textarea class="form-control" id="requestData" ></textarea>
 </div>

添加新的输入行没有问题。我已经使用淘汰赛添加了父表,并使用了jQuery添加了子表。运行良好。

问题在于生成Json子对象(城市)。 我试图获取输入值并在textare中生成Json Object,就像这样

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script th:inline="javascript">
 function createJSON() {
       var jsonObj = [];
        $(document).ready(function() {
         var valueTypeTChild = [];
         var valueTypeTParent = [];

     $("tr[id=tParentRow]").each(function() {
        $(this).find('tr[id=tChildRow]').each(function () {
             child = {
                 city: $(this).find('#cityName').val(),
                 id: $(this).find('#cityValue').val()
                      };
                  valueTypeTChild.push(child);
         });
             parent = {
                  city:$(this).find('#state').val(),
                  id:$(this).find('#value').val(),
                  child: valueTypeTChild
             };

             valueTypeTParent.push(parent);

       );

      inputItem = {
         "name":"Country",
          parents:  valueTypeTParent
      };

      jsonObj.push(inputItem);

    });
         $('#requestData').val(JSON.stringify(jsonObj));
         console.log(JSON.stringify(jsonObj));


</script> 

但是我得到了

{
"name": "USA",
"parents": [
  {
    "state": "California",
    "id": "12",
    "child": [
      {
        "city": "Los Angeles",
        "id": "1"
      },
      {
        "city": "San Francisco",
        "id": "2"
      },
      {
        "city": "Dallas",
        "id": "3"
      },
      {
        "city": "Houston",
        "id": "4"
      }
    ]
  },
  {
    "state": "Texas",
    "id": "33",
    "child": [
      {
        "city": "Los Angeles",
        "id": "1"
      },
      {
        "city": "San Francisco",
        "id": "2"
      },
      {
        "city": "Dallas",
        "id": "3"
      },
      {
        "city": "Houston",
        "id": "4"
      }
    ]
  }
]

}

1 个答案:

答案 0 :(得分:0)

您要为每个state提供相同的城市数组,而无需清除它。

var valueTypeTChild = [];循环内移动$("tr[id=tParentRow]").each(function() {