Click here to Skip to main content
14,360,161 members
Rate this:
Please Sign up or sign in to vote.
See more:
Parse my below json in C#

{
"resources": [
{
"public_id": "/OrderImages/MyImg/UploadImages/",
"type": "upload",
"created_at": "2019-07-10T11:14:42Z",
"bytes": 2560250,
"width": 2992,
"height": 2000
},
{
"public_id": "OrderImages/india/ATLANTIS THE PALM/abc1",
"type": "upload",
"created_at": "2019-07-10T11:06:50Z",
"bytes": 1454650,
"width": 2992,
"height": 2000
}
]
}

What I have tried:

parse my json in c#.net
Posted
Updated 8-Nov-19 8:57am
Rate this:
Please Sign up or sign in to vote.

Solution 1

If you look carefully, you will find tools on the internet which will do that and create your classes for you - there are quite a few. I use this one: json2csharp - generate c# classes from json[^] which is free and gave me this set of C# classes:
public class Resource
{
    public string public_id { get; set; }
    public string type { get; set; }
    public DateTime created_at { get; set; }
    public int bytes { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class RootObject
{
    public List<Resource> resources { get; set; }
}
Which (to be honest) anyone using JSON should have been able to create for themselves, given there is no complexity there at all!
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

See example here: Working With JSON String In C#[^]

If your needs are simple, you can also just use the string, see webrequest example here: How to post JSON to a server using C#? - Stack Overflow[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can use Newtonsoft.Json NuGet package. It contains a class JsonConvert which can serialize or de-serialize C# objects into JSON data. The code will look like below:
public class Resource
    {
        public string public_id { get; set; }
        public string type { get; set; }
        public DateTime created_at { get; set; }
        public int bytes { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            
            List<Resource> resources = new List<Resource>();
            resources.Add(new Resource()
            {
                public_id = "/OrderImages/MyImg/UploadImages/",
                type = "upload",
                created_at = DateTime.Now,
                bytes = 234,
                width = 10,
                height = 20
            });

            string json=  JsonConvert.SerializeObject(resources);
            Console.Write(json);

         //   [{"public_id":"/OrderImages/MyImg/UploadImages/","type":"upload","created_at":"2019-11-08T13:51:54.00859-05:00","bytes":234,"width":10,"height":20}]

            List<Resource> parsedResources= JsonConvert.DeserializeObject<List<Resource>>(json);


        }
    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100