Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Double Quotes at Begin & End from JSON Object/String or Java script Variable?

I'm getting a JSON Array of objects from servlet and trying to populate in a table control in java script.

Here is my code, for some reason it is putting double quotes at the beginning and End, which is not accepted by Table control for populating values. how can I remove this double quotes at beginning and End.

 aData = [{"A":"one","B":"Two","C":"Three","D":"8","E":"No","F":"Business","G":"0",
 "L1H":"Analytics"},{"A":"ones","B":"Twos","C":"Threes","D":"85","E":"Nos",
 "F":"BusinessD","G":"0","L1H":"AnalyticsM"}]

 var oModel = new sap.ui.model.json.JSONModel();
 oModel.setData({modelData: aData});
 var oTable=sap.ui.getCore().byId("id1");
 oTable.setModel(oModel);
 oTable.bindRows("/modelData"); // This static code of aData is working fine in
                                // my Table   control of HTMl page.

 //Here, i Wanted to get values dynamically from servlet and populate it in Table.
  var global;
  $.get('someServlet', function(data) { 
 var abc, xyz;
for(var i=0;i<(data.length);i++){
 abc='{'+'\"A\":'+'\"'+data[i].A+'\"'+','+'\"B":'+'\"'+data[i].B+'\"'+',
 '+'\"C\":'+'\"'+data[i].C+'\"'+','+'\"D\":'+'\"'+data[i].D+'\"'+',
 '+'\"E\":'+'\"'+data[i].E+'\"'+','+'\"F\":'+'\"'+data[i].F+'\"'+',
 '+'\"G\":'+'\"'+data[i].G+'\"'+','+'\"H\":'+'\"'+data[i].H+'\"}';   
        if (xyz===undefined)
            xyz=abc;
        else                
        xyz=abc+','+xyz;
            global = xyz;
        }
        global="["+global+"]";
        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData({modelData: global});
        var oTable=sap.ui.getCore().byId("id1");
        oTable.setModel(oModel);
        oTable.bindRows("/modelData");

    });
     //global="[{"A":"one","B":"Two","C":"Three"}...]"
     //alert(global);  Displaying without double quotes as expected.
     //when I see the value in Chrome debugger double quotes are appearing at begin&End

So Finally I have value in global variable is, with double quotes.

//global="[{"A":"one","B":"Two","C":"Three","D":"8","E":"No","F":"Business","G":"0","L1H":"Analytics"},

{"A":"ones","B":"Twos","C":"Threes","D":"85","E":"Nos","F":"BusinessD","G":"0","L1H":"AnalyticsM"}]"

how can I get rid of this double quotes at beginning and end of this resultSet JSONArray Objects? If I put Alert, it is displaying without double Quotes. when I see this global variable in Chrome debugger, it is showing with Double quotes and failing to populate values in Table control. I'm having bit hard time with my code in populating values into Table control which are coming from Servlet in JSON format/String/Array. Please help.

Appreciate of any input and help.

like image 683
user2682165 Avatar asked Aug 28 '13 21:08

user2682165


People also ask

How do you remove double quotes from the beginning and end of a string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do you get rid of double quotes in text?

Option 1: Remove any double quotes in a text string with replace('my string','"',''). This will substitute any instance of a double quote anywhere in the string with an empty string. Option 2: Remove the first and last character in a string with substring('my string',1,sub(length('my string'),2)).


1 Answers

You could call JSON.parse to parse your string into an object at the very end, that is:

global=JSON.parse("["+global+"]");

But instead of building yourself a string of JSON on the fly and then parsing it, it may just be simpler to set var global = []; and in your for loop do:

global.push({ 
    Deals: data[i].Deals, 
    L1H: data[i].L1H, 
    L2H: data[i].L2H 
});

Have you tried the following?

$.get('someServlet', function(data) { 
    var oModel = new sap.ui.model.json.JSONModel();
    oModel.setData({modelData: data});
    var oTable=sap.ui.getCore().byId("id1");
    oTable.setModel(oModel);
    oTable.bindRows("/modelData");
});
like image 173
sgbj Avatar answered Sep 24 '22 04:09

sgbj