5

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.

4

6 回答 6

3

您可以在最后调用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");
});
于 2013-08-28T21:33:36.767 回答
2

不要自己构建或解析 json。有一些方法可以为您做到这一点。

如果您返回的 json 只有一个具有三个属性DealsL1H和的对象数组L2H,那么data这就是您想要的。如果返回的 json 具有更多属性,而您只需要这三个属性,请改为执行以下操作:

function(data) {

    var arr = $.map(data, function(item, idx) {
        return {
            Data: item.Data,
            L1H: item.L1H,
            L2H: item.L2H
        };
    });
}
于 2013-08-28T21:34:43.363 回答
1

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

var global;    
$.get('someServlet', function(data) {
    global = data;
    populateTable(global);  // You could just as easily pass the data variable here
});
于 2013-08-28T21:30:29.147 回答
0

试试这个:

var myJSON = eval( data );

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

于 2013-08-28T22:03:14.243 回答
0

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

于 2013-08-28T21:27:19.637 回答
-1

La forma Correcta de eliminar las comillas es con

var obJason = eval( dataString );

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

应用功能eval()

obj= eval( obj);

lo transforma al obejeto deseado de Tipo Json

于 2016-11-19T14:25:19.713 回答