0

I am having some trouble deserializing a Json string to use in an api wrapper I am currently writing. This is the json string in question:

{
   "Search":[
      {
         "Title":"Clerks.",
         "Year":"1994",
         "imdbID":"tt0109445",
         "Type":"movie"
      },
      {
         "Title":"Clerks II",
         "Year":"2006",
         "imdbID":"tt0424345",
         "Type":"movie"
      }
   ]
}

This is the method I am currently using to process it.

public static Dictionary<string, string> FetchTitlesListDictionary(string searchQuery)
{
    string searchResult = SendRequest(new[] { "?s=", searchQuery });
    JObject parser = JObject.Parse(searchResult);
    var movieDictionary = new Dictionary<string, string>();
    for (int i = 0; i < parser["Search"].Count(); i++)
    {
        if (!movieDictionary.ContainsKey((string)parser["Search"][i]["imdbID"]))
        {
            movieDictionary.Add((string)parser["Search"][i]["imdbID"],
                (string)parser["Search"][i]["Title"]);
        }
    }
    return movieDictionary;
}

Even though the code above works, I feel it could, somehow, be made simpler.

How would I go about achieving this?

Thanks!

2 Answers 2

1
var obj = JsonConvert.DeserializeObject<RootObject>(searchResult);

public class Search
{
    public string Title { get; set; }
    public string Year { get; set; }
    public string imdbID { get; set; }
    public string Type { get; set; }
}

public class RootObject
{
    public List<Search> Search { get; set; }
}

If you really want to convert the RootObject to a dictionary, you can use

var movieDictionary = obj.Search.ToDictionary(s => s.imdbID, s => s.Title);

PS: see this site

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer and the link! The reason why I would rather go with a dictionary though is due to the fact that I intend to reuse this code in future projects. So, I need this class to be self-contained.
0

Well, if you're open to not using the default serializer, I would just open up the package manager console and type

Install-Package ServiceStack.Text

Then it becomes as easy as

var myDictionary = JsonObject.Parse(myJsonString)
          .ArrayObjects("Search")
          .ToDictionary(key => key.Get("imdbID"), value => value.Get("Title"));

There might be a similar and just as simple way to do it with the newtonsoft serializer, but honestly I never use it, so i'm not sure.

Also same thing as EZI's answer, it's also really easy if you have the objects he created, then you can just

var myObject = myJsonString.FromJson<RootObject>();

//if you want the dictionary it's the same thing, standard linq
var dictionary = myObject.Search.ToDictionary(x => x.imdbId, x => x.Title);

but if all you need is a dictionary, I would just use the above method, it's fast and easy.

1 Comment

Thank you! I decided to go with the package suggested by you. It worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.