I'm trying to understand the best way to set up a class in .NET 3.5. I have a JSON object that's a collection of Order Shipments defined as something like this:
[
{
"BaseOrder": {
"order_id": "503",
"BillingAddress": {
"bill_address_id": "984",
"order_id": "503"
},
"Lookup": [
{
"lookup_id": "4070",
"order_id": "503"
},
{
"lookup_id": "4071",
"order_id": "503"
}
],
"ShippingAddress": {
"ship_address_id": "983",
"order_id": "503"
}
},
"BaseOrderShipment": {
"shipment_id": "535",
"order_id": "503"
},
"BaseOrderShipmentLineitem": [
{
"line_item_id": "820",
"order_id": "503",
"shipment_id": "535"
},
{
"line_item_id": "821",
"order_id": "503",
"shipment_id": "535"
}
]
},
{
"BaseOrder": {
"order_id": "5030",
"BillingAddress": {
"bill_address_id": "9840",
"order_id": "5030"
},
"Lookup": [
{
"lookup_id": "40700",
"order_id": "5030"
},
{
"lookup_id": "40710",
"order_id": "5030"
}
],
"ShippingAddress": {
"ship_address_id": "9830",
"order_id": "5030"
}
},
"BaseOrderShipment": {
"shipment_id": "5350",
"order_id": "5030"
},
"BaseOrderShipmentLineitem": [
{
"line_item_id": "8200",
"order_id": "5030",
"shipment_id": "5350"
}
]
}
]
I'm not quite sure how I should set the class up. I'm planning on putting the class in it's own file and referencing it from my program.
This is what I'm planning on putting in the class file:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace MyCompany.MyProgram
{
public class RootObject
{
[JsonProperty("BaseOrder")]
public BaseOrder BaseOrder { get; set; }
[JsonProperty("BaseOrderShipment")]
public BaseOrderShipment BaseOrderShipment { get; set; }
[JsonProperty("BaseOrderShipmentLineitem")]
public IList<BaseOrderShipmentLineitem> BaseOrderShipmentLineitem { get; set; }
}
public class BaseOrders
{
[JsonProperty("order_id")]
public int OrderId { get; set; }
[JsonProperty("ShippingAddress")]
public ShippingAddress ShippingAddress { get; set; }
[JsonProperty("BillingAddress")]
public BillingAddress BillingAddress { get; set; }
[JsonProperty("Lookup")]
public IList<Lookup> Lookup { get; set; }
}
public class BaseOrderShipment
{
[JsonProperty("shipment_id")]
public int ShipmentId { get; set; }
[JsonProperty("order_id")]
public int OrderId { get; set; }
}
public class BillingAddress
{
[JsonProperty("bill_address_id")]
public int BillAddressId { get; set; }
[JsonProperty("order_id")]
public int OrderId { get; set; }
}
public class ShippingAddress
{
[JsonProperty("ship_address_id")]
public int ShipAddressId { get; set; }
[JsonProperty("order_id")]
public int OrderId { get; set; }
}
public class Lookup
{
[JsonProperty("lookup_id")]
public int LookupId { get; set; }
[JsonProperty("order_id")]
public int OrderId { get; set; }
}
public class BaseOrderShipmentLineitem
{
[JsonProperty("line_item_id")]
public int LineItemId { get; set; }
[JsonProperty("order_id")]
public int OrderId { get; set; }
[JsonProperty("shipment_id")]
public int ShipmentId { get; set; }
}
I'm creating an instance of the class using:
string fileName = @"C:\path\to\myFile.json";
var obj = ParseOrderShipments(fileName);
public static List<RootObject> ParseOrderShipments(string fileName)
{
//RootObject retObj = new RootObject();
List<RootObject> retObj = JsonConvert.DeserializeObject<List<RootObject>>(File.ReadAllText(fileName));
return retObj;
}
Is this a good approach to this, or is there a better way to do it?
JsonPropertyattributes, since the property names match the JSON element names