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

Asked

Viewed 446 times

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 for:

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; }
}

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 the object tags that exists within video.

At the end of Json it is possible to see that the object tag comes as if it were a single object with various properties. What is actually wrong, should come a array object tag...

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

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

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

        }

Logico that within Video I put a property Tag with tag name, when it converts it puts the value tag1 on the property Test class Tag.

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

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

1 answer

2


  • So I’ve used this library, but it will convert like the other, it will not guess q the tags are different objects, will do the same as I put in the question asked.

  • But then you can convert your dynamic object into whatever you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.