-1

I need to parse JSON array in my C# application. Format example of JSON is as follows:

{"time":1440827858965,"event":{
//other arrays
}

This is my code for parsing:

public class Response
{
    public MatchEvent event { get; set; } //error is here
}

public class MatchEvent
{ 

}

I can't create a parameter named "event", beacause it's a keyword.

How can I parse the array or create parameter named "event"?

FireFalcon
  • 831
  • 11
  • 23
  • Can you do without using the key word? – debatanu Aug 29 '15 at 06:55
  • did you at least read the attached dupe post? just add @ before the name... – Gusman Aug 29 '15 at 06:57
  • @user3081756 I can't change the incoming JSON format. – FireFalcon Aug 29 '15 at 06:57
  • 1
    @Gusman. But how can I parse the array? – FireFalcon Aug 29 '15 at 06:58
  • 1
    I don0't understand, just name it public MatchEvent @event { get; set; } – Gusman Aug 29 '15 at 06:59
  • 2
    This is not a duplicate of linked question, IT Captain is asking how to achieve the deserialization he needs not specifically using a keywork. The problem for him was that it is a keyword. @IT Captain: its a property or member, a parameter is in the signature of a method. Maybe change the title? – Devon Burriss Aug 29 '15 at 07:09
  • If you use Newtonsoft.Json (JSON.NET) to parse your JSON, you can just name your property whatever in the class and use the JsonProperty [serialization attribute](http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm) on it: `[JsonProperty("event")]`. – cbr Aug 29 '15 at 07:26
  • @Gusman thanks. That worked. – FireFalcon Aug 29 '15 at 07:59
  • @deebo you are right. Actually I want to parse the array that has C# keyword name and Gusman's tip helped. – FireFalcon Aug 29 '15 at 08:02
  • so @ event did it? Let me know as I didn't know that. I've used it for @ class but didn't know a serializer would honour it. @Gusman . I do think I should point out that @ event as a property name is contrary to most c# style guidelines but if that is what your team uses... team style trumps community convention. – Devon Burriss Aug 29 '15 at 08:14
  • It is, but I faced the same problem and when the REST API you are trying to access is not under your control is that or kill yourself XD – Gusman Aug 29 '15 at 08:15
  • @deebo so far it is working for json deserialization. – FireFalcon Aug 29 '15 at 08:19
  • Nice! @Gusman I learned something. By the keyword being the solution IT Caption went with, this does become a duplicate question though :) Lawyered! – Devon Burriss Aug 29 '15 at 08:24
  • @Gusman how to convert if json contains something like this: `{"1":{"c1":"customer1","c2":"customer2"},"2":{"c1":"customer1","c2":"customer2"}}`. How to get "1" and "2"? – FireFalcon Aug 29 '15 at 10:44
  • 1
    Here is a decent answer to your original question as well as a hint to any other deserialization. There are plenty of deserialization Q&A on SO: http://stackoverflow.com/questions/23203549/c-sharp-property-named-event?rq=1 – Devon Burriss Aug 29 '15 at 11:48

2 Answers2

1

You should follow the c# coding guidelines:

public class Response
{
  public MatchEvent Event { get; set; }
}

Then if you are using Json.NET (used in newer MS frameworks) you can set the formatter. For example in WebAPI:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

See this question: How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

Community
  • 1
  • 1
Devon Burriss
  • 2,497
  • 1
  • 19
  • 21
0

There are some common ways to read the Json data. You can use Dictionary<object,object> to read the data and then use <key,value> pair to get the data and hence parse the json data.

debatanu
  • 766
  • 5
  • 11