0

I'm new at JSON and I'm learning so excuse me if I'm being ignorant. I have the following JSON:

"data": [
{
  "id": 373644788,
  "description": "something ",
  "start": "2016-04-28T02:23:01+02:00",
  "end": "2016-04-28T02:23:12+02:00",
  "updated": "2016-04-28T02:24:05+02:00",
  "dur": 11000,
  "user": "John Smith",
  "project": "First Project",

},
{
  "id": 373635064,
  "description": "None",
  "start": "2016-04-28T01:43:42+02:00",
  "end": "2016-04-28T01:44:06+02:00",
  "updated": "2016-04-28T01:44:21+02:00",
  "dur": 24000,
  "user": "Drake",
  "project": "Second Project",

}]

Each of these JSON objects, which are inside the array is a separate project and I need to extract the data (ID, Name, Start, End) from each one and put them into my C# Class and they should be stored separately for each project because they will be later exported into a database which has the same columns as these properties. I created the Class Definitions:

 public class Definitions
    {
        public int id { get; }
        public string user { get; }
        public double dur { get; }
        public string project { get; }

        public override string ToString()
        {

              return  string.Format("ID: {0}, Name: {1}, Duration: {2}, Projekt: {3}", this.id, this.user, this.dur, this.project);


        }

The problem is that I can't access the properties inside my JSON.If I try to Deserialize it I get always 0 in my output on all my properties, as if I can't access these properties.

And the second question is how can I store them? I mean when they are exported to the database each of them should be automatically put into separate rows with each row having (ID,Nama,Start,End) columns. Thank you.

Deserilize:

String jsonString  = File.ReadAllText(@"D:\Json.json");
                Definitions def = JsonConvert.DeserializeObject<Definitions>(jsonString);
                Console.WriteLine(def);
2
  • Can you show us your Deserialization code??? Commented Apr 28, 2016 at 1:45
  • That is my Deserialisation code. I put my JSON into local file. And I get 4 properties all with 0 value Commented Apr 28, 2016 at 1:58

1 Answer 1

3

As your JSON has a property called data and this property is a array which holds many objects. What you need is to Deserialize from the root of the json, ie: from the data property.

You have already written your class with the same properties as in JSON this is right, what you want now is write another class for the data property.

public class DefinitionRoot 
{
    public List<Definitions> data {get; set;}
}

Now we have a class with a property called data and it holds list of our Definitions.

Now we can Deserialize as below.

var allDefinitions = JsonConvert.DeserializeObject<DefinitionsRoot>(yourJsonData);

After this step, your allDefinitons.data will have list of your project details.

Hope this is helpful

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

5 Comments

I did that right now and I don't get an error but I get only a blank screen
Or I get this: System.Collections.Generic.List`1[test2.Program+Definitions]. test2 is my namespace
You cannot write it in console... Place a debugger over their and look at the values, else you can also use watch window... Or else do this Console.WriteLine(def[0].id); you will get the ID of the first object in the array
Thanks that did it, in a watch window I see the values but how can I use them, for an example how can I write them to a file? And how can I export them into a database?
@user3613744 your allDefinitions.data will be a List type. So you can do a foreach loop on this and then access the values of each property using . property .. Since now inside the loop you are playing with each class ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.