如何在单独的Variabales中获取数组值

时间:2018-11-02 07:27:52

标签: javascript jquery ajax

我正在使用ajax并以json格式获取响应,但是我不知道如何在单个变量中分隔每种数组格式 我得到这个 响应

final_string =     [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":2,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":3,"q1":1,"q2":1,"q3":1,"q4":1,"q5":1,"q6":1,"q7":1,"q8":1,"q9":1,"q10":1,"q11":0},
    {"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":5,"q1":17,"q2":17,"q3":17,"q4":17,"q5":17,"q6":17,"q7":17,"q8":17,"q9":17,"q10":17,"q11":17}  ]

    [ {"feedback":"Ambience"},
    {"feedback":"Efficiency of Service"},
    {"feedback":"Friendly and Attentive"},
    {"feedback":"Variety in Menu"},
    {"feedback":"Presentation of Food \u0026 Beverages"},
    {"feedback":"Quality and Taste of Beverages"},
    {"feedback":"Quality and Taste of Food"},
    {"feedback":"Anticipated your needs Prior to Asking"},
    {"feedback":"Value for Money"},
    {"feedback":"We made you feel like a Valued Guest"},
    {"feedback":"Would you Recommend us to Others"}  ]

我只是想让它像

variable1 =  [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":2,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":3,"q1":1,"q2":1,"q3":1,"q4":1,"q5":1,"q6":1,"q7":1,"q8":1,"q9":1,"q10":1,"q11":0},
    {"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":5,"q1":17,"q2":17,"q3":17,"q4":17,"q5":17,"q6":17,"q7":17,"q8":17,"q9":17,"q10":17,"q11":17}  ]

variable2 =  [ {"feedback":"Ambience"},
    {"feedback":"Efficiency of Service"},
    {"feedback":"Friendly and Attentive"},
    {"feedback":"Variety in Menu"},
    {"feedback":"Presentation of Food \u0026 Beverages"},
    {"feedback":"Quality and Taste of Beverages"},
    {"feedback":"Quality and Taste of Food"},
    {"feedback":"Anticipated your needs Prior to Asking"},
    {"feedback":"Value for Money"},
    {"feedback":"We made you feel like a Valued Guest"},
    {"feedback":"Would you Recommend us to Others"}  ]

Ajax

 $.ajax({
                type: "POST",
                url: "FeedBackGraph.aspx/getFeedBackdata",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (Response) {
                    debugger;
                    console.log("Response",Response);
                    var d = Response.d; // Response
                    },
                error: function (result) {
                }
            });

我不知道如何使用JavaScript / jquery将每个变量分开?

5 个答案:

答案 0 :(得分:3)

假设您已解决无效的JSON问题,那么ES6将允许使用解构分配

const [variable1, variable2] = final_string

final_string = [
  [{
      "stars": 1,
      "q1": 0,
      "q2": 0,
      "q3": 0,
      "q4": 0,
      "q5": 0,
      "q6": 0,
      "q7": 0,
      "q8": 0,
      "q9": 0,
      "q10": 0,
      "q11": 0
    },
    {
      "stars": 2,
      "q1": 0,
      "q2": 0,
      "q3": 0,
      "q4": 0,
      "q5": 0,
      "q6": 0,
      "q7": 0,
      "q8": 0,
      "q9": 0,
      "q10": 0,
      "q11": 0
    },
    {
      "stars": 3,
      "q1": 1,
      "q2": 1,
      "q3": 1,
      "q4": 1,
      "q5": 1,
      "q6": 1,
      "q7": 1,
      "q8": 1,
      "q9": 1,
      "q10": 1,
      "q11": 0
    },
    {
      "stars": 4,
      "q1": 0,
      "q2": 0,
      "q3": 0,
      "q4": 0,
      "q5": 0,
      "q6": 0,
      "q7": 0,
      "q8": 0,
      "q9": 0,
      "q10": 0,
      "q11": 0
    },
    {
      "stars": 5,
      "q1": 17,
      "q2": 17,
      "q3": 17,
      "q4": 17,
      "q5": 17,
      "q6": 17,
      "q7": 17,
      "q8": 17,
      "q9": 17,
      "q10": 17,
      "q11": 17
    }
  ],

  [{
      "feedback": "Ambience"
    },
    {
      "feedback": "Efficiency of Service"
    },
    {
      "feedback": "Friendly and Attentive"
    },
    {
      "feedback": "Variety in Menu"
    },
    {
      "feedback": "Presentation of Food \u0026 Beverages"
    },
    {
      "feedback": "Quality and Taste of Beverages"
    },
    {
      "feedback": "Quality and Taste of Food"
    },
    {
      "feedback": "Anticipated your needs Prior to Asking"
    },
    {
      "feedback": "Value for Money"
    },
    {
      "feedback": "We made you feel like a Valued Guest"
    },
    {
      "feedback": "Would you Recommend us to Others"
    }
  ]
]

const [variable1, variable2] = final_string

console.log(variable1)
console.log(variable2)

答案 1 :(得分:1)

可能您需要这个。

    var final_string =    [ [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":2,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":3,"q1":1,"q2":1,"q3":1,"q4":1,"q5":1,"q6":1,"q7":1,"q8":1,"q9":1,"q10":1,"q11":0},
    {"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":5,"q1":17,"q2":17,"q3":17,"q4":17,"q5":17,"q6":17,"q7":17,"q8":17,"q9":17,"q10":17,"q11":17}  ],

    [ {"feedback":"Ambience"},
    {"feedback":"Efficiency of Service"},
    {"feedback":"Friendly and Attentive"},
    {"feedback":"Variety in Menu"},
    {"feedback":"Presentation of Food \u0026 Beverages"},
    {"feedback":"Quality and Taste of Beverages"},
    {"feedback":"Quality and Taste of Food"},
    {"feedback":"Anticipated your needs Prior to Asking"},
    {"feedback":"Value for Money"},
    {"feedback":"We made you feel like a Valued Guest"},
    {"feedback":"Would you Recommend us to Others"}  ]]
var pp=Object.assign({}, final_string)
var z=Object.keys(pp)
var constVar="variable";
var newObj={};
for(var i=0;i<z.length;i++)
{
    var key=constVar+z[i];
    newObj[key]=pp[z[i]];
}

enter image description here

答案 2 :(得分:1)

您正在正确获取数据,但它不是有效的json。 JSON应该是这样的,

[   

  [put your star array here],
  [put your feedback array here]

]

因此最终的响应代码将类似于

[
  [{"stars":1,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":2,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":3,"q1":1,"q2":1,"q3":1,"q4":1,"q5":1,"q6":1,"q7":1,"q8":1,"q9":1,"q10":1,"q11":0},
    {"stars":4,"q1":0,"q2":0,"q3":0,"q4":0,"q5":0,"q6":0,"q7":0,"q8":0,"q9":0,"q10":0,"q11":0},
    {"stars":5,"q1":17,"q2":17,"q3":17,"q4":17,"q5":17,"q6":17, "q7":17,"q8":17,"q9":17, "q10":17,"q11":17}  ],


[ {"feedback":"Ambience"},
    {"feedback":"Efficiency of Service"},
    {"feedback":"Friendly and Attentive"},
    {"feedback":"Variety in Menu"},
    {"feedback":"Presentation of Food \u0026 Beverages"},
    {"feedback":"Quality and Taste of Beverages"},
    {"feedback":"Quality and Taste of Food"},
    {"feedback":"Anticipated your needs Prior to Asking"},
    {"feedback":"Value for Money"},
    {"feedback":"We made you feel like a Valued Guest"},
    {"feedback":"Would you Recommend us to Others"}  ]

]

您需要在后端执行上述操作。从响应中删除final_string =,您不需要。

在成功的处理程序中,您可以执行以下操作来获得星星和反馈数组。

var variable1 = Response[0];
var variable2 = Response[1];

答案 3 :(得分:0)

您可以使用ES6的销毁

const [variable1, variable2] = final_string

答案 4 :(得分:0)

const data = final_string.match(/\[(\r\n|\r|\n|.)+?\]/g).map(s => JSON.parse(s))
const variable1 = data[0]
const variable2 = data[1]

这可能行得通,但不好。

const final_string_1 = "[[...],[...]]" 
const final_string_2 = "[...], [...]"

这种格式可能更适合解析。