-2

how to deserialize this json to objects of orders in C#

{
  "d": "{
    "success": true,
    "errorMessage": null,
    "data": {
      "Orders": [
        {
          "OrderID": 4914194,
          "FacilityID": 1398,
          "FacilityOrderID": "",
          "StatusID": "F",
          "Contract": true,
          "PermPlacement": false,
          "ShiftDate": "08/05/2019",
          "StartTime": "08:00:00",
          "EndTime": "17:00:00",
          "Meals": 30,
          "Con_StartDate": "08/05/2019",
          "Con_EndDate": "02/01/2020",
          "Locked": true,
          "LastModified": "2020-01-04 17:11:45",
          "CreatedDate": "2019-06-17 13:39:00",
          "FacilityName": "Carilion Roanoke Memorial Hospital",
          "ScheduledRegID": 0,
          "ScheduledRegName": "",
          "ClassName": "RAD3",
          "ClassDesc": "Rad Tech Specialty 2",
          "AltClassName": "",
          "AltClassDesc": "",
          "CodeName": "1",
          "ShiftNumber": 1,
          "AreaID": 30799,
          "AreaName": "Cardiology Clinic Outpatient",
          "AreaAddress1": "",
          "AreaAddress2": "",
          "AreaCity": "",
          "AreaCounty": "",
          "AreaState": "",
          "AreaZip": "",
          "LockReason": "Position is filled, awaiting credentials",
          "CancelReason": "",
          "Note": "Registered Echo Sonographer. 8 or 9 hours per day, 40 hours per week. Vascular experience helpful.",
          "IsTraveler": false,
          "Rate": 0,
          "ContractPattern": "Sun, Mon, Tue, Wed, Thu, Fri, Sat",
          "Con_Guarantee": 0,
          "Con_Weeks": 26
        }
      ]
    }"
  }
}

I have tried JObject.selectToken and can't even get to any of the tokens

var jsContents = JObject.Parse(contents)
var jsOrder = jsContents.SelectToken("d.data.Orders[0]")  // null

or

var exp = new JavaScriptSerializer().Deserialize<Order>(contents);
Jawad
  • 11,028
  • 3
  • 24
  • 37
Bernie
  • 1
  • 3

1 Answers1

0

I would recommend using online resources to convert your JSON to c# classes. I would recommend using json2charp to get the classes you would need.

Whenever you are creating classes for json, remember to create a json for each object that is enclosed in curly braces {}. Each [] becomes a List and rest mostly falls in line with string, bool, int etc.

Following is the structure that works with the JSON you posted., For this you will also need to install / use Newtonsoft.Json.

public class Order
{
    public int OrderID { get; set; }
    public int FacilityID { get; set; }
    public string FacilityOrderID { get; set; }
    public string StatusID { get; set; }
    public bool Contract { get; set; }
    public bool PermPlacement { get; set; }
    public string ShiftDate { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public int Meals { get; set; }
    public string Con_StartDate { get; set; }
    public string Con_EndDate { get; set; }
    public bool Locked { get; set; }
    public string LastModified { get; set; }
    public string CreatedDate { get; set; }
    public string FacilityName { get; set; }
    public int ScheduledRegID { get; set; }
    public string ScheduledRegName { get; set; }
    public string ClassName { get; set; }
    public string ClassDesc { get; set; }
    public string AltClassName { get; set; }
    public string AltClassDesc { get; set; }
    public string CodeName { get; set; }
    public int ShiftNumber { get; set; }
    public int AreaID { get; set; }
    public string AreaName { get; set; }
    public string AreaAddress1 { get; set; }
    public string AreaAddress2 { get; set; }
    public string AreaCity { get; set; }
    public string AreaCounty { get; set; }
    public string AreaState { get; set; }
    public string AreaZip { get; set; }
    public string LockReason { get; set; }
    public string CancelReason { get; set; }
    public string Note { get; set; }
    public bool IsTraveler { get; set; }
    public int Rate { get; set; }
    public string ContractPattern { get; set; }
    public int Con_Guarantee { get; set; }
    public int Con_Weeks { get; set; }
}

public class Data
{
    public List<Order> Orders { get; set; }
}

public class D
{
    public bool success { get; set; }
    public object errorMessage { get; set; }
    public Data data { get; set; }
}

public class RootObject
{
    public D d { get; set; }
}

and you would deserialize your json to the RootObject, like so.

    var obj = JsonConvert.DeserializeObject<RootObject>(json);
    List<Order> allOrders = obj.d.data.Orders;

Once you have deserialized your json, you can access the Orders and do what you need to do with them.

Working copy of your code

UPDATE

After looking again at your Json, the data you have for d is not an object, but simply a string. You will need to do this to deserialize correctly.

   var obj = JsonConvert.DeserializeObject<D>(JObject.Parse(json)["d"].ToString());
   var allOrders = obj.data.Orders;
Jawad
  • 11,028
  • 3
  • 24
  • 37
  • an api call var result = await Client.PostAsync(stUrl, content); var contents = await result.Content.ReadAsStringAsync(); – Bernie Jan 27 '20 at 21:26
  • @Bernie Updated the post to reflect all the fields from the JSON you posted. I updated your json to include only one Order for clarity reasons. – Jawad Jan 27 '20 at 21:43
  • now I'm getting an exception at var obj = JsonConvert.DeserializeObject(contents); – Bernie Jan 27 '20 at 21:45
  • @Bernie [See here.](https://dotnetfiddle.net/XLKy97). This is dotnetfiddle where you can execute your code. Compare your code with this to see whats not set up correctly. – Jawad Jan 27 '20 at 21:50
  • tried the fiddle it looks the same as I have now in my code but my code is still getting an exception at var obj = JsonConvert.DeserializeObject(contents); it was at least working through that code so it seems we were closer then – Bernie Jan 27 '20 at 22:24
  • Could not cast or convert from System.String to ConsoleWebAPICaller.D. – Bernie Jan 27 '20 at 22:32
  • @Bernie See the update at the bottom of the post and see if that resolves your issues. – Jawad Jan 27 '20 at 22:50
  • now at the first line i'm getting "Input string '0.00' is not a valid integer. Path 'data.Orders[0].Rate', line 43, position 20." I did notice now that we are not using [JsonProperty] – Bernie Jan 27 '20 at 22:56
  • i got that rate needed to be dec every thing is working THANKS Your Good !!!! – Bernie Jan 27 '20 at 23:02
  • You can use JsonProperty if the name you are using in Classes is different from what you have in json. in C#, you want to use Capital first letter which mostly is not the case in Jsons. – Jawad Jan 27 '20 at 23:10