Reputation: 833
i am using ajax and get a response in json format but i have no idea how to separate each array format in a single variable i am getting this Response
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"} ]
i just want to make it like
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) {
}
});
i have no idea how to separate each in a variable using JavaScript/jquery?
Upvotes: 2
Views: 83
Reputation: 1
const data = final_string.match(/\[(\r\n|\r|\n|.)+?\]/g).map(s => JSON.parse(s))
const variable1 = data[0]
const variable2 = data[1]
This could be work, but not good.
const final_string_1 = "[[...],[...]]"
const final_string_2 = "[...], [...]"
This format maybe more good for parse.
Upvotes: 0
Reputation: 44
you can use destruction of es6
const [variable1, variable2] = final_string
Upvotes: 0
Reputation: 3922
probably you need this.
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]];
}
Upvotes: 1
Reputation: 34107
You are getting data correctly, but its not a valid json. The JSON should be like this,
[
[put your star array here],
[put your feedback array here]
]
so the final response code will be something like
[
[{"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"} ]
]
You need to do the above in backend. Remove final_string =
from response, you don't need that.
And in your success handler you can do something like this to get stars and feedback array.
var variable1 = Response[0];
var variable2 = Response[1];
Upvotes: 1
Reputation: 56770
Let's assume you fix your invalid JSON problem, then ES6 would allow for using a destructuring assignment:
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)
Upvotes: 3