Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:57:21+00:00 2026-06-01T21:57:21+00:00

I have a situation where the JSON returned from a REST -service returns a

  • 0

I have a situation where the JSON returned from a REST-service returns a list of Movie-objects, all specced out with a ton of information. A couple of fields in that REST-service result changes depending on the information available.

An example: A Movie always has some screen captures (images), actors and directors. Depending on the movie in question, there might be one or more images, one or more actors and one or more directors. Sample JSON for a couple of cases:

{
    "title": "Movie title",
    "images": [
        "http://www.url.com/img_0.jpg",
        "http://www.url.com/img_1.jpg",
        "http://www.url.com/img_2.jpg",
        "http://www.url.com/img_3.jpg",
        "http://www.url.com/img_4.jpg"
    ],
    "actors": [
        "Steven Berkoff",
        "Nikolaj Coster-Waldau",
        "Julie Cox"
    ],
    "directors": "Simon Aeby"
},
{
    "title": "Another movie",
    "images": "http://www.url.com/img_1.jpg",
    "actors": "actor 1"
    "directors": [
        "Justin Bieber",
        "Justin Timberlake"
    ]
}

The question is, using JSON.net, how can I create a converter that deals with this problem? I’ve been scouring the internet, but still haven’t found a solution.

Another spin on the same question: If a field is either a List of strings or a simple string, how do I make JSON.NET create a List either way (and if just a simple string, create a list with one member)

EDIT: This REST-service is out of my control

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:57:24+00:00Added an answer on June 1, 2026 at 9:57 pm

    Ok, I did it for fun, but don’t think is useful or the best way, anyway…

    Declaring the “dynamic” attributes as object and then create methods to obtain the properties as something like ImagesAsList or ImagesAsString. I did it with Extension Methods…..

    var movies = JsonConvert.DeserializeObject<List<Movie>>(str);
    

    Class

    class Movie
    {
    
        [JsonProperty("title")]
        public string Title { get; set; }
    
        [JsonProperty("images")]
        public object Images { get; set; }
    
        [JsonProperty("actors")]
        public object Actor { get; set; }
    
        [JsonProperty("directors")]
        public object Directors { get; set; }
    }
    

    Extension Methods

    static class MovieExtension
    {
        public static List<string> ImagesAsList(this Movie m)
        {
            var jArray = (m.Images as JArray);
            if (jArray == null) return null;
    
            return jArray.Select(x => x.ToString()).ToList();
        }
    
        public static string ImagesAsString(this Movie m)
        {
            return m.Images as string;
        }
    
    }
    

    EDIT

    After reading @yamen comments I did some changes like:

    var settings = new JsonSerializerSettings();
    settings.Converters.Add(new MoviesConverter());
    
    var movies = JsonConvert.DeserializeObject<List<Movie>>(str, settings);
    

    Class

    class Movie
    {
    
        [JsonProperty("title")]
        public List<string> Title { get; set; }
    
        [JsonProperty("images")]
        public List<string> Images { get; set; }
    
        [JsonProperty("actors")]
        public List<string> Actor { get; set; }
    
        [JsonProperty("directors")]
        public List<string> Directors { get; set; }
    }
    

    Converter

    class MoviesConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(string)) || (objectType == typeof(List<string>)) ;
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.StartArray)
            {
                var l = new List<string>();
                reader.Read();
                while (reader.TokenType != JsonToken.EndArray)
                {
                    l.Add(reader.Value as string);
    
                    reader.Read();
                }
                return l;
            }
            else
            {
                return new List<string> { reader.Value as string };
            }
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            //ToDo here we can decide to write the json as 
            //if only has one attribute output as string if it has more output as list
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AJAX controller action which returns JSON. The returned JSON is handled
I'm using django-piston for my REST json api, and I have it all set
i have a situation where i need to create some json from some id's
I have a situation here. May I know how to get json from place
I have a service which returns a Json data structure. One of the properties
I have a situation where I'm returning json objects to my application which are
I have situation in which I read a record from a database. And if
I have a situation where I need to initialize/assign variables from the results of
I have the following json array of objects in my code var groups =
my situation is this: I have similar objects that differ in certain fields. Example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.