Delete Double Quotes on start and end with JSON Object / String or Java script Variable?

I get a JSON array of objects from a servlet and try to populate a control with a table in a java script.

Here is my code, for some reason it puts double quotes at the beginning and at the end, which is not accepted by the table control to populate the values. how can i remove these double quotes at the beginning and at the 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, I have a value in a global variable, with double quotes.

 //global="[{"A":"one","B":"Two","C":"Three","D":"8","E":"No","F":"Business","G":"0","L1H":"Analytics"}, 

{"A": "units", "B": "deuces", "C": "triples", "D": "85", "E": "nose", "F": "BusinessD" "G ":" 0 "," L1H ":" AnalyticsM "}]"

How can I get rid of these double quotes at the beginning and end of this result? Define JSONArray objects? If I put Alert, it is displayed without double quotes. when I see this global variable in the Chrome debugger, it appears with double quotes and does not populate the values โ€‹โ€‹in the table control. It is a little difficult for me with my code to populate the values โ€‹โ€‹in the Table control that come from the Servlet in JSON / String / Array format. Please, help.

Appreciate any input and help.

+4
source share
6 answers

You can call JSON.parse to parse your string in the object at the very end, i.e .:

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

But instead of creating the JSON string on the fly and then parsing it yourself, the easiest way is to set var global = []; and in your for loop:

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

Do not create or parse json yourself. There are methods available for this.

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

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

Your global variable is a JSON string. You do not need to create a string. As far as I can tell, data 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 }); 
+1
source

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

0
source

Try the following:

 var myJSON = eval( data ); 

where data is the string that servelt sent. The Eval function does the work, parses the string in JSON.

0
source

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

 obj= eval( obj); 

lo tranforma al obejeto deseado de tipo json

-one
source

Source: https://habr.com/ru/post/1499496/


All Articles