jQuery在嵌套对象中转换json数据数组

时间:2016-08-01 09:24:13

标签: jquery

具有多个div数据的HTML代码我尝试使用此jquery概念获取文本及其值。 我需要将代码发送到该数组结构

<div class="woocommerce">
    <div class="addon-blocks" id="adds0">
       <div class=" product-addon">
        <h3 class="addon-name">Type of page: (eg. Home, About, Product)     </h3>
        <p>
            <input type="text" value="text1" name="addon-2445-type-of-page-eg-home-about-product-0[0]" class="addon">
        </p>
    </div>
    <div class=" product-addon">
        <h3 class="addon-name">Type of pagesss </h3>
        <p>
            <input type="text" value="text2" name="addon-2445-type-of-page-eg-home-about-product-0[0]" class="addon">
        </p>
    </div>
    <div class="bulknumword">
        <h3>Number of Words</h3>
        <input type="number" name="custom_data_6[0][]" class="custom_data_6"> 
        <input type="hidden" value="1234" name="custom_data_2[0][]" class="custom_data_2" id="cusid0"> 
    </div>
</div>
</div>

<div class="woocommerce">
   <div class="addon-blocks" id="adds1">
    <div class=" product-addon">
         <h3 class="addon-name">xxxxxxxx </h3>
         <p>
             <input type="text" value="text3" name="addon-2445-xxxxxx-0[0]" class="addon">
         </p>
     </div>
     <div class=" product-addon">
         <h3 class="addon-name">x1x1x1x1x1 </h3>
         <p>
            <input type="text" value="text4" name="addon-2445-xxxxxx-0[0]" class="addon">
        </p>
    </div>
    <div class="bulknumword">
        <h3>Number of Words</h3>
        <input type="number" name="custom_data_6[0][]" class="custom_data_6"> 
        <input type="hidden" value="4567" name="custom_data_2[0][]" class="custom_data_2" id="cusid1"> 
      </div>
    </div>
    </div>
  <div class="woocommerce">
  <div class="addon-blocks" id="adds2">
    <div class=" product-addon">
        <h3 class="addon-name">yyyyyyyy </h3>
        <p>
            <input type="text" value="" name="addon-2445-yyyyyyyy-0[0]" class="addon">
        </p>
    </div>
    <div class=" product-addon">
        <h3 class="addon-name">y1y1y1y1 </h3>
        <p>
            <input type="text" value="" name="addon-2445-yyyyyyyy-0[0]" class="addon">
        </p>
    </div>
    <div class="bulknumword">
        <h3>Number of Words</h3>
        <input type="number" name="custom_data_6[0][]" class="custom_data_6"> 
        <input type="hidden" value="8910" name="custom_data_2[0][]" class="custom_data_2" id="cusid2"> 
    </div>
  </div>
</div>

$('.addon-blocks').each( function (s) {
                        len = $(this).length;
                            $('#adds' + s).find('.product-addon').each(function () {
                                $(this).siblings('.bulknumword').find('input[name^="custom_data_2"]').attr("id", "cusid" + s);
                                idsval = $('#adds' + s).find('input[name^="custom_data_2"]').val();
                                textname = $(this).find('h3.addon-name').text();
                                valtext = $(this).find(' .addon ').val();
                                five.push(textname);
                                six.push(valtext);
                            }); 
                        one.push(carts);    
                     });
                    two.push({
                                id: one,
                                addsfields : five,
                                addsnames : six
                        });

我需要以这种格式发送json结构

[{
product:{
    id: 1234,
    addnames: {
        Type of page: (eg. Home, About, Product),
        Type of pagesss
    },
    addsfields: {
        text1,
        text2
    },
},
product:{
    id: 4567,
    addnames: {
        xxxxxxxx,
        x1x1x1x1
    },
    addsfields: {
        text3,
        text4
    },
},
product:{
    id: 8910,
    addnames: {
        yyyyyyyy,
        y1y1y1y1
    },
    addsfields: {
        text3,
        text4
    },
},

}]

目前它的显示就像这个结构

[{
product:{
    id: 1234,4567, 8910
    addnames: {
        Type of page: (eg. Home, About, Product),
        Type of pagesss,
        xxxxxxxx,
        x1x1x1x1,
        yyyyyyyy,
        y1y1y1y1
    },
    addsfields: {
        text1,
        text2,
        text3,
        text4
    },
},

}]

1 个答案:

答案 0 :(得分:0)

我创建了示例应用。你能检查一下吗?这对你有帮助。

Click here.

var product={
        id: 1234,
      addnames:{ name:'nar', name:'nar1'},
      addsfields: {text:'text1', text:'text2'}
    };
var ar=[];
ar.push({"product":product});
$(document).ready(function(){
     console.log(JSON.stringify(ar));
 });