Deserialize JSON (HAL/HATEOAS) C#

Hi Experts,

I have been given an API to consume. This API follows the HATEOAS paradigm. How do I go about deserializing the below JSON. Am Currently using JSON.NET so if we could have answers with that in mind, that is preferable. If not, I'm open to suggestions.

Cheers,

Chud

{"homepage":"https://blabla.com",
"termsOfUse":"https://blabla.com",
"_links":
{
"my:info:service":"https://blabla.com/v1/my-info-service/item?jurisdiction={jurisdiction}{&channelType,salesType}",
"my:info:service:next":"https://blabla.com/v1/my-info-service/item/next?jurisdiction={jurisdiction}",
"my:info:service:tomorrow":"https://blabla.com/v1/my-info-service/item/tomorrow?jurisdiction={jurisdiction}
}}
chudmarekAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
The documentation for JSON.NET shows you how to deserialize. The only bit you need to tweak is to account for the property names with colons in them. You can overcome this by tagging each affected property with a JsonPropertyAttribute, passing in the name expected to be found in the JSON (i.e. the name with the colons).
chudmarekAuthor Commented:
Ok, that's fine, but what if the _links " {"property":"value"} is dynamic is there any way I can deserialize the property/value combo in _links as a list say, or to a key/value store, Dictionary etc?
käµfm³d 👽Commented:
You could use a dictionary, sure. You'd still have an issue with not knowing what keys are in the dictionary at compile time. Maybe this is of no consequence, though--I don't know your environment.

e.g.

using System.Collections.Generic;
using Newtonsoft.Json;

namespace _28749279
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"{""homepage"":""https://blabla.com"",
                             ""termsOfUse"":""https://blabla.com"",
                             ""_links"":
                             {
                                             ""my:info:service"":""https://blabla.com/v1/my-info-service/item?jurisdiction={jurisdiction}{&channelType,salesType}"",
                             ""my:info:service:next"":""https://blabla.com/v1/my-info-service/item/next?jurisdiction={jurisdiction}"",
                             ""my:info:service:tomorrow"":""https://blabla.com/v1/my-info-service/item/tomorrow?jurisdiction={jurisdiction}""
                             }
                            }";

            Dictionary<string, object> deserialized = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
        }
    }
}

Open in new window


The use of object also means you lose type safety.
chudmarekAuthor Commented:
what if i only wanted the contents of the _links as a dictionary, the rest of the json to have their properties intact, like so:

[JsonObject(MemberSerialization.OptIn)]
class MyJsonClass
{
  [JsonProperty]
        public string homepage { get; set; }
        [JsonProperty]
        public string termsOfUse { get; set; }
        [JsonProperty]
        public Dictionary<string,object> _links { get; set; }
}
käµfm³d 👽Commented:
You could do that.

Screenshot

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.