Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a class like below

class Test
    {
        public long TEstId{ get; set; }

        
        public IEnumerable<(long? issueId, long[] newChildIds)> OrderChanges { get; set; }

}

When am passing the data as below to above model, ienumberable property returns null

{
  "TEstId": 179,
  "OrderChanges ": [
    {
      "item1": 12,
      "item2": [
        10
      ]
    }
  ]
}


Could someone help me to resolve this.
Thanks is advance.

DK

What I have tried:

I tried in web, but i dont get any useful info.
Posted
Updated 28-Jan-20 0:36am
Comments
Richard Deeming 28-Jan-20 14:18pm    
What library are you using to deserialize your JSON? I've just tried with JSON.NET v12, and after removing the space from the "OrderChanges" property name, your JSON correctly deserializes into your class.

Demo[^]

NB: I had to set the demo to compile as .NET Core, because the DotNetFiddle site doesn't like value tuples with the .NET Framework compiler, and won't let you add NuGet references with the Roslyn compiler. But I've tried the same code in LINQPad 5 running against .NET Framework 4.8, and it works.

C# supports Tuples, but ... JSON doesn't have a tuple indicator, so it "does the best it can" and generates a type that isn't a tuple. If I correct the JSON (by removing the space from the name "OrderChanges ") and run it through a JSON class generator[^] you get this:
public class OrderChange
{
    public int item1 { get; set; }
    public List<int> item2 { get; set; }
}

public class RootObject
{
    public int TEstId { get; set; }
    public List<OrderChange> OrderChanges { get; set; }
}
Which is probably why you get a null - the classes just don't match.

I'd check where you got the JSON from - if it thinks it's exporting tuples, then it needs to be looked at.
 
Share this answer
 
Comments
Member 14103699 28-Jan-20 7:04am    
Thanks for the response. But i need to go with IEnumerable as per our design.
OriginalGriff 28-Jan-20 7:13am    
Then you will need to change the JSON, and probably the tool that creates it.
I'm amazed you found no useful information about how to deserilise JSON, there are lots of articles.

public class OrderChange
{
    public long item1 { get; set; }
    public long[] item2 { get; set; }
}

public class Test
{
    public long TEstId { get; set; }


    public IEnumerable<OrderChange> OrderChanges { get; set; }
}
 
Share this answer
 
v2
Comments
Member 14103699 28-Jan-20 7:02am    
Is there any possibility to proceed without creating the new class
F-ES Sitecore 28-Jan-20 7:08am    
Your OrderChanges would need to be defined like this

public IEnumerable<Tuple<long?, long[]>> OrderChanges { get; set; }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900