0
Thats my class.

class MyClass1 : IInterface1
{
    public MyClass1()
    {
        this.Interface2s = new List<IInterface2>();
    }

    public string strI1 { get; set; }
    public int intI1 { get; set; }
    public IList<IInterface2> Interface2s { get; set; }
    public int intC1 { get; set; }
}

In application I serialized it with some random values. Result:

{
  "strI1": "strI1",
  "intI1": 2,
  "Interface2s": [
    {
      "intI2": 111
    },
    {
      "intI2": 222
    },
    {
      "intI2": 333
    }
  ]
}

Then, i want to deserialize that string back, but I'm loosing values in my IList

result of deserialization next:
{
  "strI1": "strI1",
  "intI1": 2,
  "Interface2s": [
    {
      "intI2": 0
    },
    {
      "intI2": 0
    },
    {
      "intI2": 0
    }
  ]
}

To deserialize interface I'm using that example http://www.newtonsoft.com/json/help/html/DeserializeWithDependencyInjection.htm

I need to deserialize values in list too. Any suggestions?

Rise Against
  • 23
  • 10
  • 1
    What do you use to serialize your class into json? – Kim Hoang Dec 21 '16 at 09:16
  • There are a very large number of these questions on stackoverflow already. Does none of those answers help you? Please try, and I am sure you can figure it out – jumps4fun Dec 21 '16 at 09:18
  • 2
    Possible duplicate of [Convert Json String to C# Object List](http://stackoverflow.com/questions/22191167/convert-json-string-to-c-sharp-object-list) – jumps4fun Dec 21 '16 at 09:20
  • Or [Convert JSON String To C# Object](http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object) or [How to Convert JSON object to Custom C# object?](http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) or about 100 other questions. Please check to see if a question has been asked already before asking. – Liam Dec 21 '16 at 09:22
  • Can you show IInterface2. Btw deserialization to interface is problem. – Mertuarez Dec 21 '16 at 09:56
  • Interface2 has one property intI2 {get;set;}, thats all – Rise Against Dec 21 '16 at 11:05

1 Answers1

2

My advice is just use http://www.newtonsoft.com/json

JsonConvert.SerializeObject(target)

and to deserialize

JsonConvert.DeSerializeObject<MyClass1>(target)

And it should just be as simple as that.

gmn
  • 4,199
  • 4
  • 24
  • 46
  • Sorry for poor example, but I'm using json.net and there is the problem – Rise Against Dec 21 '16 at 11:05
  • 'JsonConvert.DeSerializeObject(target)' returns me MyClass and List in it with 3 empty objects. – Rise Against Dec 21 '16 at 11:10
  • Try experimenting with http://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm during serialization. That might be the issue, it could be deserializing the interface to the wrong type, so by embedding type information, that could solve your issue. @RiseAgainst – gmn Dec 21 '16 at 11:24
  • I found the issue, but not solution... The issue is that json deserealize interface, so there is no values. I nedd him to deserialize class, that inherit my interface. – Rise Against Dec 21 '16 at 11:30
  • Yes @RiseAgainst thats why you need to tell Json.Net to add the type information when it serializes. Then it will know which types to deserialize – gmn Dec 21 '16 at 11:42