Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
{"orderMode":"20",

"nonFabricLineItemDetails":[{"itemID":"NF2881","quantity":"45"}],
"orderRefNo":"393_manisha_1465553838","courierId":"365",

"fabricLineItemDetails":
[{"itemID":"F31793","quantity":"1.00"},{"itemID":"F19170","quantity":"23.00"}],

"customerID":393}


this is a json that i will get from a source. This is an Order placed by a single customer. but the order is of 2 types fabric and nonfabric.
Each type consists of items . Now i want to divide them as :

FABRIC ORDER and Non FAbric order from the above json along with common tags of customerid courier id etc.

What I have tried:

Created this class .

[DataContract]
public class Order
{

[DataMember(EmitDefaultValue = false)]
public string orderMode { get; set; }

[DataMember(EmitDefaultValue = false)]
public string itemID { get; set; }

[DataMember(EmitDefaultValue = false)]
public string quantity { get; set; }

[DataMember(EmitDefaultValue = false)]
public string orderRefNo { get; set; }

[DataMember(EmitDefaultValue = false)]
public string courierId { get; set; }

[DataMember(EmitDefaultValue = false)]
public string customerID { get; set; }

}
Posted
Updated 23-Jun-16 5:33am

1 solution

Your class structure needs to match the json structure otherwise the deserialiser doesn't know what properties to populate with what

C#
[DataContract]
public class Order
{
    [DataMember(EmitDefaultValue = false)]
    public string orderMode { get; set; }

    [DataMember]
    public List<Item> nonFabricLineItemDetails { get; set; }
        
    [DataMember]
    public List<Item> fabricLineItemDetails { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string orderRefNo { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string courierId { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string customerID { get; set; }

}

[DataContract]
public class Item
{
    [DataMember(EmitDefaultValue = false)]
    public string itemID { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string quantity { get; set; }
}
 
Share this answer
 

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