Question39820425 c-sharp-how-convert-json-string-to-class

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;		
					

	public class Reported
	{
		public int BlockerId { get; set; }
		public string Title { get; set; }
		public int Date { get; set; }
		public int ForfeitCount { get; set; }
		public int RewardCount { get; set; }
	}
	
	public class CommentDtoAdmin
	{
		public int UserId { get; set; }
		public string UID { get; set; }
		public string Title { get; set; }
		public string Sender { get; set; }
		public string Answer { get; set; }
		public string Comment { get; set; }
		public object ProductTitle { get; set; }
		public int CommentId { get; set; }
		public string Logo { get; set; }
		public int Date { get; set; }
		public int AnswerDate { get; set; }
		public bool AnswerEdit { get; set; }
		public bool CommentEdit { get; set; }
		public int ForfeitCount { get; set; }
		public int RewardCount { get; set; }
		public int ThisCountReport { get; set; }
		public List<Reported> Reported { get; set; }
		public int Gem { get; set; }
	}
	
	public class Result
	{
		public int dateTime { get; set; }
		public List<CommentDtoAdmin> list { get; set; }
	}
	
	public class RootObject
	{
		public Result Result { get; set; }
		public string StatusCode { get; set; }
		public string Description { get; set; }
	}
					
	public class Program
	{
		static string GetJson()
		{
			var json = @"{
    ""$id"": ""1"",
    ""Result"": {
        ""$id"": ""2"",
        ""dateTime"": 23821964,
        ""list"": [{
            ""$id"": ""3"",
            ""UserId"": 302,
            ""UID"": ""302_UID"",
            ""Title"": ""شیدکو"",
            ""Sender"": ""شیدکو"",
            ""Answer"": """",
            ""Comment"": ""test 2"",
            ""ProductTitle"": null,
            ""CommentId"": 77,
            ""Logo"": ""http://www.asankharid.ir/persianCommercial/User/302/Logo/tmpF0BF.jpg"",
            ""Date"": 24302057,
            ""AnswerDate"": -2123661683,
            ""AnswerEdit"": false,
            ""CommentEdit"": false,
            ""ForfeitCount"": 0,
            ""RewardCount"": 0,
            ""ThisCountReport"": 2,
            ""Reported"": [{
                ""$id"": ""4"",
                ""BlockerId"": 355,
                ""Title"": ""محتوای غیر اخلاقی"",
                ""Date"": -19527396,
                ""ForfeitCount"": 0,
                ""RewardCount"": 0
            }, {
                ""$id"": ""5"",
                ""BlockerId"": 355,
                ""Title"": ""محتوای غیر مرتبط"",
                ""Date"": -19527382,
                ""ForfeitCount"": 0,
                ""RewardCount"": 0
            }],
            ""Gem"": 0
        }, {
            ""$id"": ""6"",
            ""UserId"": 302,
            ""UID"": ""302_UID"",
            ""Title"": ""شیدکو"",
            ""Sender"": ""شیدکو"",
            ""Answer"": """",
            ""Comment"": ""test 2"",
            ""ProductTitle"": null,
            ""CommentId"": 77,
            ""Logo"": ""http://www.asankharid.ir/persianCommercial/User/302/Logo/tmpF0BF.jpg"",
            ""Date"": 24302057,
            ""AnswerDate"": -2123661683,
            ""AnswerEdit"": false,
            ""CommentEdit"": false

        }]

    },
    ""StatusCode"": ""Created"",
    ""Description"": null
}";
			return json;
		}
		
		public static void Main()
		{
			var json1 = GetJson();
			
			var settings = new JsonSerializerSettings
			{
				PreserveReferencesHandling = PreserveReferencesHandling.Objects,
			};
			var root = JsonConvert.DeserializeObject<RootObject>(json1, settings);
			
			var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
			
			Console.WriteLine(json2);
			
			var queryable = root.Result.list.AsQueryable();
		}
	}
;