-1

I'm generating a JavaScript object in a Java class for use in javascript the result looks like this:

var gridDefinition      = {"width":"100%",
"height":700,
"sortable":true,
"columns"[{"datafield":"id","datatype":"string","width":300,"hidden":true,"text":"ID"},
{"datafield":"lastname","datatype":"string","cellsrenderer":"renderer_openEntry","width":300,"text":"Nachname"},
{"datafield":"firstname","datatype":"string","width":200,"text":"Vorname"},
{"datafield":"officePhoneNumber","datatype":"string","width":150,"text":"Telefon"},
{"datafield":"companyName","datatype":"string","width":300,"text":"Firma"},
{"datafield":"mailServer","datatype":"string","width":200,"text":"Mail-Server"},
{"datafield":"mailFile","datatype":"string","width":400,"text":"Mail-Datei"}]} ;

cellsrenderer is a callback function name. How can I remove the doublequotes surrounding renderer_link? The result line should look like this:

{"datafield":"lastname","datatype":"string","cellsrenderer":renderer_openEntry,"width":300,"text":"Nachname"},
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 2
    If you remove the quotes then it's not JSON anymore. You'll want to post-process this parsed JSON object in your Javascript, resolving a string which holds the name of a function to the actual function reference. Can you provide a little more context so we can help you with concrete code? – deceze Jan 06 '17 at 14:34
  • @deceze It's not JSON, and never has been. It's a JavaScript object literally being assigned to a variable. – user229044 Jan 06 '17 at 14:35
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Jan 06 '17 at 14:35
  • 1
    *"How can I remove the doublequotes surrounding renderer_link?"* What if I tell you that there are no quotes? At the time of execution `gridDefinition` is an object, and `gridDefinition.columns[x].cellsrenderer` is a *string* value. At that moment there are no quotes you can "remove". You are basically asking how to interpret the value of a string as a variable name. The only sensible answer to that is to have a map that maps a name/string to the variable and look up the value in that map. – Felix Kling Jan 06 '17 at 14:36
  • @meagar I know. I take it OP is producing JSON and is printing it into their JS source. I'm saying that they won't be able to do that if they want to "remove the quotes", since then it's not JSON, and they can't simply plunk it into the JS source, and they'd have to dynamically produce *Javascript source code* instead, which is a whole 'nother cattle of fish you don't typically want to get into. – deceze Jan 06 '17 at 14:37

1 Answers1

1

How to "remove the quotes" is the wrong question to ask. At the moment the code is running, there are no quotes that can be removed.

What you need is a map that maps a name to a variable, e.g.

var myFunctions = {
  renderer_openEntry: renderer_openEntry,
  // other functions
};

and then process the object to update the values of all cellsrender proeprties to refer to the value from the map instead of the string:

gridDefinition.columns.forEachfunction(column) {
  if (column.cellsrenderer) {
    column.cellsrenderer = myFunctions[column.cellsrenderer];
  }
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143