Skip to content

Getting object properties dynamically #178

Answered by niloc132
faveoled asked this question in Q&A
Discussion options

You must be logged in to vote

The https://github.com/google/jsinterop-base/ project provides some tools that can help here - the simplest would be Js.asPropertyMap(parsed), which would return a jsinterop.base.JsPropertyMap<Object> instance. This doesn't directly provide a way to read all keys, but you can query known keys as Object or as jsinterop.base.Any instances.

Object parsed = JSON.parse("{\"a\":\"b\"}");
JsPropertyMap<Object> contents = Js.asPropertyMap(parsed);
// Using Any lets you put in runtime checks for the property type
String value = contents.getAsAny("a").asString();
// You can also directly read the value and cast
String checked = (String) contents.get("a");
String checked2 = Js.cast(contents.get("a"));

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@faveoled
Comment options

Answer selected by faveoled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants