Friday, December 16, 2016

Example Deserialize a Json Array using Newtonsoft.Json

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace Rextester
{

public class MyClass
{
    public List<Item> data;
}
    public class Item
    {
        public string Comment;
        public string Name;
        public string CreationDate;
        public string Url;
        public string Uuid;
    }
    
    public class Program
    {
        static string json =@"
{
    'data': [
        {
            'comment': '<No comment>',
            'creationDate': '14.07.2016 22:14',
            'name': 'Name version 1',
            'url': 'http:\\\\www.google.com',
            'uuid': '12345'
        },
        {
            'comment': 'Hotfix. ',
            'creationDate': '14.07.2016 22:13',
            'name': 'Name version 2',
            'url': 'http:\\\\www.google.com',
            'uuid': '8888888'
        },
        {
            'comment': 'for test purposes',
            'creationDate': '14.07.2016 13:34',
            'name': 'Name version 3',
            'url': 'http:\\\\www.google.com',
            'uuid': 'hiuhihiuphpuihiuh'
        }
    ]
}
";
        
        public static void Main(string[] args)
        {
            //Your code goes here
            
            MyClass ss = JsonConvert.DeserializeObject<MyClass>(json);
            Console.WriteLine(ss.data[0].Comment);
            
        }
    }
}