最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove Double Quotes at Begin & End from JSON ObjectString or Java script Variable? - Stack Overflow

programmeradmin2浏览0评论

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 ing from Servlet in JSON format/String/Array. Please help.

Appreciate of any input and help.

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 ing from Servlet in JSON format/String/Array. Please help.

Appreciate of any input and help.

Share Improve this question edited Aug 28, 2013 at 23:17 user2682165 asked Aug 28, 2013 at 21:20 user2682165user2682165 631 gold badge2 silver badges7 bronze badges 3
  • 1 So it looks like your ajax returns json, but you're trying to strip out all but three properties, so you're doing manual json string building, and trying to pass that to a function? Is that right? – Jason P Commented Aug 28, 2013 at 21:27
  • 1 wow. i didn't realize that's what he was doing. that's silly. – Kevin B Commented Aug 28, 2013 at 21:28
  • You can use stringify to convert the json to string. – Archit Saxena Commented Aug 28, 2013 at 21:29
Add a ment  | 

6 Answers 6

Reset to default 3

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");
});

Don't build or parse json yourself. There are methods available to do it for you.

If your returned json only has an array of objects with the three properties Deals, L1H, and L2H, then data is what you want. If the returned json has more properties, and you only want those three, do this instead:

function(data) {

    var arr = $.map(data, function(item, idx) {
        return {
            Data: item.Data,
            L1H: item.L1H,
            L2H: item.L2H
        };
    });
}

Your global variable is a JSON string. You don't need to construct a string. As far as I can tell, data is already a JavaScript object. I think this is what you want:

var global;    
$.get('someServlet', function(data) {
    global = data;
    populateTable(global);  // You could just as easily pass the data variable here
});

Since you are usig jQuery, try http://api.jquery./jQuery.parseJSON/ and it will return you an object instead.

Try with this:

var myJSON = eval( data );

where data is the string that the servelt has sent. Eval function makes the work, parse a string into JSON.

La forma correcta de eliminar las illas es con

var obJason = eval( dataString );

var obj= "[{"ID":"786-000X-XX8","NAME":"LISANDRO ARCILES"}]";

aplicado la funcion eval()

obj= eval( obj);

lo tranforma al obejeto deseado de tipo Json

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论