从JSON对象/字符串或Java脚本变量中删除开头和结尾的双引号?

时间:2022-09-15 16:18:49

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

我从servlet获取JSON数组对象并尝试在java脚本中填充表控件。

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.

这是我的代码,由于某种原因,它在开头和结尾放置双引号,Table控件不接受填充值。如何在开头和结尾删除这个双引号。

 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.

如何在这个resultSet JSONArray对象的开头和结尾摆脱这个双引号?如果我放置警报,它显示没有双引号。当我在Chrome调试器中看到这个全局变量时,它会显示Double引号并且无法填充Table控件中的值。我在使用我的代码时很难将值填充到Table控件中,这些控件来自Servlet的JSON格式/ String / Array。请帮忙。

Appreciate of any input and help.

感谢任何输入和帮助。

6 个解决方案

#1


3  

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

你可以调用JSON.parse将你的字符串解析成最后一个对象,即:

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:

但是,不是动态地构建自己的JSON字符串然后解析它,设置var global = []可能更简单;并在你的for循环中:

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

#2


2  

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

不要自己构建或解析json。有一些方法可以帮到你。

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:

如果返回的json只有一个包含三个属性Deals,L1H和L2H的对象数组,那么数据就是你想要的。如果返回的json具有更多属性,并且您只需要这三个属性,请执行以下操作:

function(data) {

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

#3


1  

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:

您的全局变量是JSON字符串。您不需要构造字符串。据我所知,数据已经是一个JavaScript对象。我想这就是你想要的:

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

#4


0  

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

既然你是usig jQuery,请尝试http://api.jquery.com/jQuery.parseJSON/,它会返回一个对象。

#5


0  

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.

其中data是servelt发送的字符串。 Eval函数使工作,将字符串解析为JSON。

#6


-1  

La forma correcta de eliminar las comillas es con

La forma correcta de eliminar las comillas es con

var obJason = eval( dataString );

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

aplicado la funcion eval()

aplicado la funcion eval()

obj= eval( obj);

lo tranforma al obejeto deseado de tipo Json

lo tranforma al obejeto deseado de tipo Json

#1


3  

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

你可以调用JSON.parse将你的字符串解析成最后一个对象,即:

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:

但是,不是动态地构建自己的JSON字符串然后解析它,设置var global = []可能更简单;并在你的for循环中:

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

#2


2  

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

不要自己构建或解析json。有一些方法可以帮到你。

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:

如果返回的json只有一个包含三个属性Deals,L1H和L2H的对象数组,那么数据就是你想要的。如果返回的json具有更多属性,并且您只需要这三个属性,请执行以下操作:

function(data) {

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

#3


1  

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:

您的全局变量是JSON字符串。您不需要构造字符串。据我所知,数据已经是一个JavaScript对象。我想这就是你想要的:

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

#4


0  

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

既然你是usig jQuery,请尝试http://api.jquery.com/jQuery.parseJSON/,它会返回一个对象。

#5


0  

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.

其中data是servelt发送的字符串。 Eval函数使工作,将字符串解析为JSON。

#6


-1  

La forma correcta de eliminar las comillas es con

La forma correcta de eliminar las comillas es con

var obJason = eval( dataString );

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

aplicado la funcion eval()

aplicado la funcion eval()

obj= eval( obj);

lo tranforma al obejeto deseado de tipo Json

lo tranforma al obejeto deseado de tipo Json