gpt4 book ai didi

javascript - 从 JSON 对象/字符串或 Java 脚本变量中删除开始和结束处的双引号?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:40 27 4
gpt4 key购买 nike

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

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

 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

所以最后我在全局变量 is 中有值,用双引号引起来。

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

如何去掉这个 resultSet JSONArray 对象开头和结尾的双引号?如果我放置警报,它会在没有双引号的情况下显示。当我在 Chrome 调试器中看到这个全局变量时,它显示为双引号并且无法在表控件中填充值。我在将值填充到表控件时遇到了一些困难,这些值来自 JSON 格式/字符串/数组的 Servlet。请帮忙。

感谢任何输入和帮助。

最佳答案

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

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

但是与其动态地构建一个 JSON 字符串然后解析它,不如设置 var global = []; 并在您的 for 循环中执行以下操作可能更简单:

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

您尝试过以下方法吗?

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

关于javascript - 从 JSON 对象/字符串或 Java 脚本变量中删除开始和结束处的双引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18498413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com