Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deserialize an JSON object with invalid field name in it

I have a JSON request which has follwing structure:

"formats":  {
        "flash_embed": "http://a3.vikiassets.com/assets/vikiplayer-922746a667cfd38137a7e45df6ba1b95.swf?auto_play=true&language_codes=en&media_id=74965&partner=16&source=api_v3",
        "m3u8": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965/ios.m3u8",
        "res-150p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_mp4cell_150.mp4",
        "res-240p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_240p.mp4",
        "res-270p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_270p.mp4",
        "res-360p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_360p.mp4",
        "res-480p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_480p.mp4",
        "res-720p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_720p.mp4"
      }
    }

Now res-150p, says invalid name in C# and if I give another name to it then while desiralizing I am not getting any values, that is null inside res-150p.

Edit: [Serializable] MoviesListRootObject is the root Object which Contains Response and then Response Contains Formats

public class MoviesListRootObject
{
    public int count { get; set; }
    public Pagination pagination { get; set; }
    public List<Response> response { get; set; }
}

[Serializable]
public class Response
{
    public int id { get; set; }
    public int channel_id { get; set; }
    public string title { get; set; }
    public string title_language { get; set; }
    public string description { get; set; }
    public string description_language { get; set; }
    public string created_at { get; set; }
    public string uri { get; set; }
    public string web_uri { get; set; }
    public List<object> genres { get; set; }
    public string origin_country { get; set; }
    public string image { get; set; }
    public Subtitles subtitles { get; set; }
    public Formats formats { get; set; }
}

[Serializable]
public class Formats
{
    public string flash_embed { get; set; }
    public string m3u8 { get; set; }
    public string __invalid_name__res150p { get; set; }
    public string __invalid_name__res240p { get; set; }
    public string __invalid_name__res270p { get; set; }
    public string __invalid_name__res360p { get; set; }
    public string __invalid_name__res480p { get; set; }
    public string __invalid_name__res720p { get; set; }
    public string __invalid_name__flv480p { get; set; }
    public string __invalid_name__flv360p { get; set; }
    public string __invalid_name__flv270p { get; set; }
    public string __invalid_name__flvvp6360p { get; set; }
    public string __invalid_name__flvvp6270p { get; set; }
}
like image 393
Simsons Avatar asked Aug 31 '12 12:08

Simsons


People also ask

How do I deserialize an object in JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is Jsonproperty attribute?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

What is serialize and deserialize in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

What is System JSON deserialize?

The System. Text. Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.


1 Answers

You have to decorate your Formats properties with JsonProperty attributes to tell it what goes where if the name does not exactly match:

partial class Formats
{
    [JsonProperty("res-150p")]
    public string __invalid_name__res150p {get; set;}
}

See also Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

like image 182
lc. Avatar answered Oct 01 '22 01:10

lc.