nested key in bindings

João Paulo Varandas joaovarandas at inpaas.com
Wed Sep 21 14:55:00 UTC 2016


Hi Murat.

For a structure like this:
var obj = { "key1": { "key2" : { "key3": "value1-2-3" } } }


Simple answer:
The get call support flat structures.

b.get("obj.key1.key2.key3") will return null ...

But ...


Long answer:
If you are in JavaScript, you can evaluate this with the syntax:
var val = obj.key1.key2.key3;

If you are in Java, what happens is that you have a Map, inside a Map,
inside a Map ...

Map<String, Object> map1 = (Map<String, Object>) obj.get("key1");
Map<String, Object> map2 = (Map<String, Object>) map1.get("key2");
Object val = (Map<String, Object>) map2.get("key3");

Since you cannot chain the calls, you either need a helper method where you
will "chain" this in a recursive method, or you may ask the scriptEngine to
evalute this for you!

Have you tried something like ...
myScriptEngine.eval( "myobj.key1.key2.key3", bindings )
?

See this gist:
https://gist.github.com/joaovarandas/57ff0b539e9fbf82974959dfb168b468


2016-09-21 11:20 GMT-03:00 Murat Cetin <mcetin71 at gmail.com>:

> Hi,
>
> I read a .js file that results in Bindings that have a nested key value
> structure, e.g, key1: value1, value1 : [key2, key3, key4], key2: [value2,
> value3], and etc. Bindings.get(Object) will only return key1..I cannot
> chain multiple get calls and I don't see a method to get to say key2.. Am I
> doing something wrong? or does the get call only support flat structures?
>
> thanks,
> Murat
>

-- 
"Esta mensagem, incluindo seus anexos, pode conter informacoes 
confidenciais e privilegiadas. 
Se voce a recebeu por engano, solicitamos que a apague e avise o remetente 
imediatamente. 
Opinioes ou informacoes aqui contidas nao refletem necessariamente a 
posicao oficial da Plusoft."

"Antes de imprimir, pense em sua responsabilidade e compromisso com o MEIO 
AMBIENTE"



More information about the nashorn-dev mailing list