Home > front end >  how to get value from the JSON Arrary AJAX Response
how to get value from the JSON Arrary AJAX Response

Time:12-05

I want to get value of retVal

{"result":[[{"RetVal":"1"}],30]}

I have tried the following but it is showing undefined

success: function (data) {
                alert(JSON.stringify(data));
                var result = data.result;

                alert(result.RetVal);
                alert(result[0][0].retVal);
                alert(result[0].RetVal);

CodePudding user response:

To get the value of the RetVal property from the JSON data you provided, you can try using the following code:

success: function (data) {
  alert(JSON.stringify(data)); // this line is optional, it just displays the entire JSON data in an alert

  var result = data.result; // this gets the "result" property from the JSON data
  var retVal = result[0][0].RetVal; // this gets the "RetVal" property from the first element in the first element of the "result" array

  alert(retVal); // this displays the value of the "RetVal" property in an alert
}

In the code above, data.result returns the result property of the JSON data, which is an array with two elements:

  1. A nested array containing a single object with a RetVal property
  2. A number (30 in this case)

To get the RetVal property from the first element of the result array, we access it using the following syntax: result[0][0].RetVal.

Note that property names in JavaScript are case-sensitive, so you need to use the correct capitalization when accessing the RetVal property (i.e., RetVal, not retVal).

  •  Tags:  
  • json
  • Related