How to convert json to object data Widget API

Can any one let me know how we can convert Json data like ({
"Forum":{"Roles":"","Status":"","Group":""},
"Blogs":{"Roles":"","Type":"","Group":""},
"Events":{"Roles":"","Type":""},
"TechTalks":{"Roles":"","Type":""},
"Webinars":{"Roles":"","Type":""},
"Known Issue":{"Roles":""},
"Learning Paths":{"Roles":"","Type":""},
"Licensing":{"Roles":""},
"Release Notes":{"Roles":""},
"Stack OverFlow":{"Roles":"","Type":""},
}

to object or how we can use json data in wodget api

Parents
  • Hi Mohit,

    I don't think there is anything out of the box to do this, as I believe everything returned by the default APIs are concrete objects and you couldn't do that from a generic JSON string.

    I'd probably recommend creating a custom extension that deserialized the JSON string to a known object, or even better combining the retrieval of the JSON string and deserializing into one custom extension method so you can do error checking etc. in one place, but without knowing more about your specific problem hard to say for sure.

    You could create a custom extension that takes in a JSON string and deserializes and then use the result, as I was curious if this would work have given it a quick go and seems to be OK (code below), I haven't fully tested this and don't think I'd recommend this in many scenarios as seperating the retrieval and parsing of this type of data in the widget code can often make the widget code more complicated than it needs to be and make the error handling and parsing problematic, but if it is what you need to do then from a quick look seems to work (you'd need to test it more thoroughly and add any additional logic required for your problem)

    using Newtonsoft.Json;
    using Telligent.Evolution.Extensibility.UI.Version1;
    using Telligent.Evolution.Extensibility.Version1;
    
    namespace ArdourDigital.TelligentCommunity.JsonToObject
    {
        public class JsonToObjectPlugin : IScriptedContentFragmentExtension, IPlugin
        {
            public string ExtensionName => "ardour_v1_jsonToObject";
    
            public object Extension => new JsonDeserializer();
    
            public string Name => "Ardour Digital - Convert JSON to Object";
    
            public string Description =>  "EXPERIMENTAL - Allows a JSON string to be deserialized to an object that can be used in widgets";
    
            public void Initialize()
            {
            }
        }
    
        public class JsonDeserializer
        {
            public object Deserialize(string data)
            {
                return JsonConvert.DeserializeObject(data);
            }
        }
    }
    

    #set ($data = "{ 'example' : 123, 'test': { 'something' : 'This is an example', 'value': 123.45  } }")
    
    #set ($result = $ardour_v1_jsonToObject.Deserialize($data))
    
    $result.example
    
    $result.test.something
    
    $result.test.value

    Hope this helps,
    Rhys

Reply Children
No Data