Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a JSON string that would cause illegal C# identifiers?

Tags:

json

c#

json.net

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.

like image 782
TaLha Khan Avatar asked Jul 02 '14 16:07

TaLha Khan


People also ask

What happens if you JSON parse a string?

Description. JSON.parse() parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression.

What error does JSON parse () throw when the string to parse is not valid JSON?

JSON. parse() is a built-in method in JavaScript which is used to parse a JSON string and convert it into a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

What is JSON parse exception?

JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.

What is a valid JSON string?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.


2 Answers

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); 
like image 70
L.B Avatar answered Sep 30 '22 12:09

L.B


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; } } 
like image 32
ken2k Avatar answered Sep 30 '22 12:09

ken2k