How to convert Json to Object where column name is a variable number using HttpResponseMessage

1

I have the following Json:

{"video":{"duration":"23:16","views":"2358","video_id":"1235288","rating":"5.00","ratings":"3","title":"titulo do video","url":"urldovideo","default_thumb":"urlaqui","thumb":"outra url aqui","publish_date":"2015-08-26 23:00:34","tags":{"58":"tag1","320":"tag2","74":"tag3"}}}

I need to convert to an Object, I'm mapping the object video to:

public class RootObject
{
  public Videointerto video { get; set; }
}
public class Videointerto
{
  public string duration { get; set; }
public string views { get; set; }
public string video_id { get; set; }
public string rating { get; set; }
public string ratings { get; set; }
public string title { get; set; }
public string url { get; set; }
public string default_thumb { get; set; }
public string thumb { get; set; }
public string publish_date { get; set; }
}

It works perfectly using the following code:

public static RootObject GetApiObject(string url)
        {

            HttpClient client;
            Uri usuarioUri;

            client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {

                var x = response.Content.ReadAsAsync<RootObject>().Result;

                return x;

            }

            throw new NotImplementedException();
        }

The problem is when it arrives at object tags that exists inside video .

At the end of Json you can see that the tag object comes as if it were a single object with several properties. What is actually wrong, should be array of objects tag ...

"tags":{"58":"tag1","320":"tag2","74":"tag3"}}}

At the test level I made an evolution of class Videointerio creating a class Tag as follows:

public class Tags
        {
            [JsonProperty("58")]
            public string Teste;

        }

Logic that within Video I put a Tag property with tag names, when it converts it it places the tag1 value in the Test property of the Tag class.

Only this value number is a key of the object in the company database that I need to get Json.

I was wondering if it is possible to do either of two things, or make the tags part of the Json come in a single string or make it come as a array of the Tag class (if there is any way to map)

    
asked by anonymous 24.09.2015 / 20:41

1 answer

2

The JSON.NET package does this for you .

Example:

dynamic d = JObject.Parse(response.Content.ReadAsAsync<RootObject>().Result);

The documentation is here

    
24.09.2015 / 21:07