Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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 7:57am

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:
C#
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!
 
Share this answer
 
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[^]
 
Share this answer
 
v2
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);


        }
    }
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900