Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Accessing json data from DataDictionary

    Cinema 4D SDK
    c++ r20 r21 r23 s22 s24
    2
    3
    558
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • kbarK
      kbar
      last edited by kbar

      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

      https://www.gamelogicdesign.com
      https://www.plugins4d.com

      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by

        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

        MAXON SDK Specialist

        MAXON Registered Developer

        1 Reply Last reply Reply Quote 2
        • kbarK
          kbar
          last edited by

          That is what I was looking for. Thanks Manuel!

          https://www.gamelogicdesign.com
          https://www.plugins4d.com

          1 Reply Last reply Reply Quote 0
          • First post
            Last post