[DataContract]
class A
{
[DataMember]
public Int32 P1 { get; private set; }
[DataMember]
public String P2 { get; private set; }
}
{
"serializable": true, // этого поля нет в классе
"P1": 1,
"P2": "ffda"
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace ConsoleApplication11
{
class Test
{
public string P1 { get; set; }
public int P2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var str = JsonConvert.SerializeObject(new List<object> {
new { s = true, P1 = "one", P2 = 12 },
new { s = false, P1 = "two", P2 = 13 },
new { s = true, P1 = "three", P2 = 14 } });
dynamic dObject = JsonConvert.DeserializeObject(str);
var list = new List<Test>();
foreach (var item in dObject)
{
if (item.s == true)
{
list.Add(item.ToObject<Test>());
}
else
{
Console.WriteLine("No way");
}
}
Console.ReadKey();
}
}
}