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?

6
  • 1
    What do you use to serialize your class into json? Commented Dec 21, 2016 at 9: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 Commented Dec 21, 2016 at 9:18
  • 2
    Possible duplicate of Convert Json String to C# Object List Commented Dec 21, 2016 at 9:20
  • Or Convert JSON String To C# Object or How to Convert JSON object to Custom C# object? or about 100 other questions. Please check to see if a question has been asked already before asking. Commented Dec 21, 2016 at 9:22
  • Can you show IInterface2. Btw deserialization to interface is problem. Commented Dec 21, 2016 at 9:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for poor example, but I'm using json.net and there is the problem
'JsonConvert.DeSerializeObject<MyClass1>(target)' returns me MyClass and List in it with 3 empty objects.
Try experimenting with 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
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.