locked
Eliminate Quotes RRS feed

  • Question

  • User571301025 posted

    I have tried to fill the Javascript Object's property values with dynamic values. But whenever i filled it will put inside Quotes (").

    This is my JS Object:

    var person = {
        firstName:"John",
        lastName:"Doe",
        age:50,
        eyeColor:"blue"
    };

    I have tried to add some new property values (which is fetched from Webmethod call).

    var salaryPropValues= values (values come from webmethod like [100,200,300,400,500])

    After that i embed that prop into JS Object like below:

    person.Salary = salaryPropValues;

    My problem is, the SalaryPropValues are enclosed with Quotes like below.

    var person = {
        firstName:"John",
        lastName:"Doe",
        age:50,
        eyeColor:"blue",

        Salary: "[100,200,300,400,500]"
    };

    I want to eliminate the Quotes. How can i ?

     

     

     

    Monday, May 18, 2015 10:34 AM

Answers

  • User571301025 posted

    Thanks Krunal. I have solved the issue with "eval". It will returns the output as i expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 24, 2015 10:59 AM

All replies

  • User753101303 posted

    Hi,

    So it seems you are using a string when you want to use an array (though I'm not sure what means a salary array rather than  a single numeric value for a yearly or monthly salary). My understanding is that this property comes first from a web method? What is the return type for this web method?

    The point is not to eliminate quotes but rather to avoid having them to start with which should be possibly by using the proper data type.

    Monday, May 18, 2015 10:51 AM
  • User571301025 posted

    Thanks for the reply Patrice.

    The web method return last 5 month of salary in below format as "string" type.

    [100,200,125,100,500]

    I grab this value and assigned this to JS variable. Then try to assign that JS variable to JS Object property. At that time alone the property value put inside Quotes.

    var strWebmethodValue = valueFromWebMethod; //[100,200,125,100,500]

    person.Salary = strWebMethodValue;

    The above one will give the result as:

    person.Salary ="[100,200,125,100,500]";

    But i want this to be like below:

    person.Salary =[100,200,125,100,500];

    How can i do this?

    Monday, May 18, 2015 11:46 PM
  • User1644755831 posted

    Hello farncissvk,

    francissvk

    The web method return last 5 month of salary in below format as "string" type.

    Since you are returning the string from the web Method it will be enclosed in the "" Quotes. You need to make your web method return type List<int> or int[]. then on the client side the parsing of json in string and string into json should be done automatically by the asp.net.

    Here is an example

    ASPX

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm32.aspx.cs" Inherits="WebFormsIdentity.WebForm32" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script src="Scripts/jquery-2.1.3.js"></script>
      <script type="text/javascript">
        $(document).ready(function () {
    
          $.ajax({
            type: "POST",
            url: "WebForm32.aspx/ReturnArray",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
              $.each(data.d, function () {
                $('#result').append($("<span></span>").html(this));
                $('#result').append($('<br/>'));
              });
            },
            error: function (response) {
              alert(response.d);
            }
          });
    
          $.ajax({
            type: "POST",
            url: "WebForm32.aspx/ReturnListArray",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
              $.each(data.d, function () {
                $('#result').append($("<span></span>").html(this));
                $('#result').append($('<br/>'));
              });
            },
            error: function (response) {
              alert(response.d);
            }
          });
        });
    
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
        <div id="result">
        </div>
      </form>
    </body>
    </html>

    CS

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsIdentity
    {
      public partial class WebForm32 : System.Web.UI.Page
      {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        [WebMethod]
        public static int[] ReturnArray()
        {
          return new int[] { 100, 200, 300, 400, 500 };
        }
    
        //or
        [WebMethod]
        public static List<int> ReturnListArray()
        {
          return new List<int> { 600, 700, 800, 900, 1000 };
        }
      }
    }

    Result

    In JavaScript you will get now array instead of the string

    Now you can assign it success of the method.

      success: function (data) {person.Salary = data.d; },
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    Tuesday, May 19, 2015 10:49 PM
  • User571301025 posted

    Thanks Krunal. I have solved the issue with "eval". It will returns the output as i expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 24, 2015 10:59 AM