48

I have been using NewtonSoft JSON Convert library to parse and convert JSON string to C# objects. But now I have came across a really awkward JSON string and I am unable to convert it into C# object because I cant make a C# class out of this JSON string.

Here is the JSON string

{
"1": {
    "fajr": "04:15",
    "sunrise": "05:42",
    "zuhr": "12:30",
    "asr": "15:53",
    "maghrib": "19:18",
    "isha": "20:40"
},
"2": {
    "fajr": "04:15",
    "sunrise": "05:42",
    "zuhr": "12:30",
    "asr": "15:53",
    "maghrib": "19:18",
    "isha": "20:41"
 } 
}

The C# class required to parse this JSON string should be like this:

public class 1 {

    public string fajr { get; set; }
    public string sunrise { get; set; }
    public string zuhr { get; set; }
    public string asr { get; set; }
    public string maghrib { get; set; }
    public string isha { get; set; }
}

public class 2 {

    public string fajr { get; set; }
    public string sunrise { get; set; }
    public string zuhr { get; set; }
    public string asr { get; set; }
    public string maghrib { get; set; }
    public string isha { get; set; }
}

But it cant be a true C# class because we know that Class names cannot start with a number.

It will be really great if anyone can suggest how to parse such type of json string.

5
  • 2
    Your logic is wrong. from what I see, you have a list of items with the same structure. So your class should represent that structure, NOT the list, and you should parse your JSON to a .NET List, not to several classes. Commented Jul 2, 2014 at 16:40
  • 2
    As soon as it looks like key-value pairs, the answer below is spot on Commented Jul 2, 2014 at 16:40
  • @Bartdude list or NOT the list? Commented Jul 2, 2014 at 16:41
  • Yeah there is another class which has the two properties one for class 1 and other for class 2 Commented Jul 2, 2014 at 16:42
  • 1
    L.B. here unders explained this much more clearly than me :-) Commented Jul 2, 2014 at 16:43

3 Answers 3

66

You can deserialize to a dictionary.

public class Item
{
    public string fajr { get; set; }
    public string sunrise { get; set; }
    public string zuhr { get; set; }
    public string asr { get; set; }
    public string maghrib { get; set; }
    public string isha { get; set; }
}

var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a ton..worked like a charm......problem solved. but still i dont know how it worked. can you please explain how dictionary worked ?
@TaLhaKhan Because your json represents key-value pairs. "1" is the key, {...} is the value.
48

While the dictionary is the best solution for the specific case you had, the question you asked could also be interpreted as:

how do I deserialize objects with property names that cannot be used in C#?

For example what if you had

{
    "0": "04:15",
    "zzz": "foo"
}

Solution: use annotations:

public class Item
{
   [JsonProperty("0")]
   public string AnyName { get; set; }

   [JsonProperty("zzz")]
   public string AnotherName { get; set; }
}

3 Comments

Of course, if the number of Keys and their names are known beforehand. Not suitable for dynamically created objects like userid : {name:aa,surname:bb}, anotheruserid:{}
@I4V If they are dynamically created objects you probably couldn't use predefined classes anyway.
@R0MANARMY I didn't mean it . Keys can be dynamic, for example a guid or an id comming from db. But its value is a known type. {{1232574:{name:john}, 755344674:{name:doe}}
0

You can use this extensions:

public static class JsonExtensions
{
    public static Dictionary<string, T> ToObject<T>(this string json)
    {
        return JsonConvert.DeserializeObject<Dictionary<string, T>>(json);
    }

    public static string ToJson<T>(this T obj)
    {
        return JsonConvert.SerializeObject(obj);
    }
}

Comments

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.