简体   繁体   中英

Cannot create javascript array from JSON response

I am using Jquery and AJAX to call a web method in my C# code behind, which runs a stored procedure and then sends the data back as a string. This is all working perfectly.

When I have my values back and I try make an array out of them so I can select only certain objects out of the array, everything falls apart. I have made a quick FIDDLE of the problem I am having.

I receive a string like this from my Stored Procedure: 12, 1288, 1800, 3088, 26288

But when I try get the "12" only, I either receive the whole string or just the "1" from the 12.

I have tried everything from makeArray, stringify, split, replacing then splitting, making an array variable and push() into it ... nothing seems to work.

Below is some of my scrap code which I have been commenting out as I try and I've also added an image of the code behind while stepping through:

 $.ajax({
                      type: "POST",
                      url: "LeadGraphGeneration.aspx/GetData",
                      contentType: "application/json; charset=utf-8",
                      data: strRequest,
                      dataType: "json",
                      success: function (msg) {
                          var returned = (msg.d);
                          //                          var returned = new Array();
                          //                          $(msg.d).each(function () {
                          //                              returned.push(this);
                          //                          });

                          ret1 = returned[1];

                          //                          var returnedd = JSON.stringify(msg.d);
                          //                          var returned = $.makeArray(returnedd);



                          //                          var ret1 = returned[0];
                          //                          var ret2 = returned[1];
                          //                          var ret3 = returned[2];
                          //                          var ret4 = returned[3];
                          //                          var ret5 = returned[4];


                          //data1 = dataArray[0];


                          $("#PaymentPeriod").text(ret1);
                          //                          $("#TotalInterest").text(ret2);
                          //                          $("#TotalFees").text(ret3);
                          //                          $("#TotalCost").text(ret4);
                          //                          $("#TotalPayment").text(ret5);

在此处输入图片说明 (Right click, open in new tab for full size) I'm hoping you could help me figure out why I can't select individual objects from the array which I am struggling to build.

Please let me know if you need any more info.

"but when I try get the "12" only, I either receive the whole string or just the "1" from the 12.."

If your string is "12, 1288, 1800, 3088, 26288" and it's in msg.d , then:

var returned = msg.d.split(/\s*,\s*/);

The regular expression tells split to split the string on any run of optional whitespace characters followed by a comma followed by optional whitespace characters.

var returnedd = "12, 1288, 1800, 3088, 26288"
var rett = $.makeArray(returnedd);

makeArray will not convert a string in that format to an array, rather use split :

var rett = returnedd.replace(/\s/g, "").split(",");
alert(rett[0])

As for

ret2 = returnedd[0];

This is calling [0] on a string which will give you the first char (just like .charAt(0) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM