Answer the question
In order to leave comments, you need to log in
JSON deserialization control?
How to manage deserialization?
There is a class
[DataContract]
class A
{
[DataMember]
public Int32 P1 { get; private set; }
[DataMember]
public String P2 { get; private set; }
}
{
"serializable": true, // этого поля нет в классе
"P1": 1,
"P2": "ffda"
}
Answer the question
In order to leave comments, you need to log in
Something like this:
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();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question