Accessing json data from DataDictionary
-
I have been looking at the docs for DataDictionary and I can't seem to find way to access data by a key. Probably just missing something simple.
I am loading in JSON data into a DataDictionary. Then I want to access it via something similar to this...
dict = DataDictionary;
String name = dict.Get("name").GetString()which would return Bob for something like this
{
"name" : "Bob"
}Note that I know this is not the actual way you access the data, but this is how I would like to access it. IE can I use a string as the key to access the data?
So far all I have found is some example code that requires iterating over the entire DataDictionary and forces you to compare every entry against what you want. Which is terribly inefficient and not how you would normally deal with json data.
So I am just thinking I haven't found the right syntax that I need to use to access this data yet.
I have a feeling that maybe this JSON Parser and DataDictionary combo that is in the Maxon API is not actually a replacement at all for dealing with JSON data.
Any help appreciated.
Cheers,
Kent -
Hi,
As you know, everything is stored inside DataDictionnary. In your case you can do
dict.Get<String>("name"_s) iferr_return;
So yes, you can use a string as a key.If you have various levels of data, you can retrieve them with
BaseArray<Data> users = dict.Get<BaseArray<Data>>("users"_s)
"Users": [ { "name": "toto", "value": [ sldfk, sldfj, sdlj] }, { "name": "tata", "value": [ sldfk, sldfj, sdlj] } ]
Convert the Data to DataDictionnary
for (auto const& data : users) { // we know they are dictionary so we can convert Data to Dictionary DataDictionary user = data.Convert<DataDictionary>() iferr_return; const String name = user.Get<String>("name"_s) iferr_return; // ... }
I don't know if it's the best approach or not, but we are not going to change that for now. You can open a suggestion on BL.
I hope this helped. Let me know otherwise.Cheers,
Manuel -
That is what I was looking for. Thanks Manuel!