在javascript中循环json变量

时间:2017-12-06 05:50:37

标签: javascript jquery json ajax

我有一个变量,其中我得到<%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %> 值。该值来自4个表。我想在分配值时循环每个表。以下是我的代码

json

我从表中获取值为

var dataResponse = JSON.parse(response); dataResponse.Table[0] ....

如何在javascript中循环它。请帮忙

我的dataResponse.Table[1]

json

更新

{
  "Table": [{
    "JOB_PROGRESS_ID": 4.0,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU",
    "LINK_ID": "12345678",
    "SPAN_TYPE": "INTERCITY",
    "NE_SPAN_LENGTH": 200.0,
    "HOTO_OFFERED_LENGTH": 26.0,
    "LIT_OFFERED_LENGTH": 30.0,
    "HOTO_ACTUAL_LENGTH": 20.0,
    "LIT_ACTUAL_LENGTH": 15.0,
    "HOTO_OFFERED_DATE": null,
    "HOTO_ACCEPTENCE_DATE": null,
    "LIT_ACCEPTENCE_DATE": null,
    "APPROV_REJECT_REMARK": null,
    "ISABDMISSING": 1.0,
    "JOB_ID": 1.0,
    "MAINTENANCEZONECODE": "INMUKLYN01"
  }],
  "Table1": [{
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.09,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": null,
    "ASBUILT_LENGTH": 0.33,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 12.41,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.01,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.46,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 8,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.02,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 6,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.33,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "CLAMP",
    "ASBUILT_LENGTH": 0.48,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 5,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.14,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 8,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 5.45,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": null,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.23,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }],
  "Table2": [],
  "Table3": [{
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }]
}

3 个答案:

答案 0 :(得分:1)

哦,我的... CREATE TABLE "PROD_INDEX" ( "PNO" NUMBER (4,0) NOT NULL ENABLE, "CATEGORY" VARCHAR(15) , "PRODUCT" VARCHAR2(10), "COUNTRY" VARCHAR2(9), "BRAND" VARCHAR(10)), "DATE" DATE, "YEAR" NUMBER(4,0), "COST" NUMBER(7,2), "SALES" NUMBER(10,2), PRIMARY KEY ("PNO") ENABLE ) / 不是dataResponse.Table。这是Array

对于Object类似数组的对象,使用for loop

但是,要循环Array,请使用for...in loop

在您的情况下,可以这样做:

Object

我不会直接回答你的问题。因为很明显你没有在研究javascript中的循环方面做出任何显着的努力。

答案 1 :(得分:0)

响应是对象而不是数组。 对于迭代对象,我们使用 for-in 循环和 为了迭代数组,我们使用进行循环。 在您的情况下,您使用for-in循环。

for(var item in dataResponse){
//logic
}

答案 2 :(得分:-1)

var dataResponse = {
  "Table": [{
    "JOB_PROGRESS_ID": 4.0,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU",
    "LINK_ID": "12345678",
    "SPAN_TYPE": "INTERCITY",
    "NE_SPAN_LENGTH": 200.0,
    "HOTO_OFFERED_LENGTH": 26.0,
    "LIT_OFFERED_LENGTH": 30.0,
    "HOTO_ACTUAL_LENGTH": 20.0,
    "LIT_ACTUAL_LENGTH": 15.0,
    "HOTO_OFFERED_DATE": null,
    "HOTO_ACCEPTENCE_DATE": null,
    "LIT_ACCEPTENCE_DATE": null,
    "APPROV_REJECT_REMARK": null,
    "ISABDMISSING": 1.0,
    "JOB_ID": 1.0,
    "MAINTENANCEZONECODE": "INMUKLYN01"
  }],
  "Table1": [{
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.09,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": null,
    "ASBUILT_LENGTH": 0.33,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 12.41,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 3,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.01,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.46,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 8,
    "ROUTETYPE": "HDD",
    "ASBUILT_LENGTH": 0.02,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 6,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.33,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 7,
    "ROUTETYPE": "CLAMP",
    "ASBUILT_LENGTH": 0.48,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 5,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.14,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": 8,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 5.45,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }, {
    "NUMBEROFDUCTS": null,
    "ROUTETYPE": "OT",
    "ASBUILT_LENGTH": 0.23,
    "SPAN_ID": "MUMUMBMUKLYNSPN001_BU"
  }],
  "Table2": [],
  "Table3": [{
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }, {
    "UMS_GROUP_ASS_BY_NAME": "Construction_Eng",
    "MODIFIED_BY": "HARDIK1.SHAH",
    "MODIFIED_DATE": "2017-11-27T12:07:39",
    "APPROVED_DATE": null,
    "REJECTED_DATE": null
  }]
}
for(count in dataResponse){
 console.log(`[[[[[[[[[[[[[${count}]]]]]]]]]]]]]]`);
 for(item of dataResponse[count]){
    console.log(item);
  }
}

  

您可以使用dataResponsefor-of轻松获得for-in的价值。

首先,您必须使用object name获取for-in

其次,您可以控制表的名称。 (表,表1 ......)

for(item of dataResponse[table])将返回每个表的值。