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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18498413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:47:03  来源:igfitidea点击:

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

javascriptjsonjqueryjavascript-framework

提问by user2682165

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.

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

 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"}]"

{"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 调试器中看到这个全局变量时,它显示为双引号并且无法在 Table 控件中填充值。我的代码很难将值填充到来自 JSON 格式/字符串/数组的 Servlet 的表控件中。请帮忙。

Appreciate of any input and help.

感谢任何输入和帮助。

采纳答案by sgbj

You could call JSON.parseto 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");
});

回答by Jason P

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 datais what you want. If the returned json has more properties, and you only want those three, do this instead:

如果返回的JSON只与三个属性对象的数组DealsL1HL2H,然后data是你想要的。如果返回的 json 具有更多属性,而您只需要这三个属性,请改为执行以下操作:

function(data) {

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

回答by zigdawgydawg

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

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

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

回答by ronnyfm

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

由于您使用 jQuery,请尝试http://api.jquery.com/jQuery.parseJSON/,它会返回一个对象。

回答by Brian

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 是服务器发送的字符串。Eval 函数完成工作,将字符串解析为 JSON。

回答by Lisandro Arciles

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()

应用程序 eval()

obj= eval( obj);

lo tranforma al obejeto deseado de tipo Json

lo tranforma al obejeto deseado de tipo Json