-1

I have a Json file that looks like this

{
  "itemDetails": [
    {
      "quantity": 2,
      "name": "1 Hour Massage",
      "description": null,
      "code": null,
      "price": 44.99,
      "value": 49.99,
      "expiresOn": null,
      "expiresInMonths": null,
      "overrideExpiry": false,
      "sku": "",
      "id": null
    }
  ],
  "purchaserEmail": "jane@example.com",
  "purchaserName": "Jane Smith",
  "recipientDetails": {
    "recipientName": "Tommy Smith",
    "recipientEmail": "tommy@example.com",
    "message": "Happy Holidays!",
    "scheduledFor": "2020-12-25T00:00:00"
  },
  "disableAllEmails": null,
  "orderDate": null
}

how would i go about dezerializing this Json i have tried making objects for the arrays like this

namespace Api
{
    class Order
    {


        ItemDetails [] itemDetails { get; set; }
        string purchaserEmail { get; set; }
        string purchaserName { get; set; }

        bool disableAllEmails { get; set; }
        string orderDate { get; set; }
    }

and like this

using System;
using System.Collections.Generic;
using System.Text;

namespace Api
{
    class ItemDetails
    {
        int quantity {get; set;}
        string name{get; set;}
        string description { get; set; }
        string code { get; set; }
        double price { get; set; }
        double value { get; set; }
        string expiresOn { get; set; }
        string expiresInMonths { get; set; }
        bool overrideExpiry { get; set; }
        string sku { get; set; }
        string id { get; set; }
    }
}

but this does not work??? how would i do this the biggest problem for me is the array cuz the rest i get it's just that i can't reach the data of the aray Is there a way to use ReadAsASync to do this?

  • I believe your json shows an array of `ItemDetails` but your class only has a single object. Try changing `itemDetails` to `ItemDetails[] itemDetails { get; set; }` and see if that fixes it. – dvo Jan 07 '20 at 13:50
  • Saying "does not work" isn't helpful for us. Why doesn't it work? Do yuo get errors? Is your output wrong? In what way is it wrong? – DavidG Jan 07 '20 at 13:55
  • If you are not sure what classes to create, use aany website that converts json to c# classes, json2csharp.com – Jawad Jan 07 '20 at 14:08
  • i am using ReadAsAsync to deserialize is there a way to do that? –  Jan 07 '20 at 14:18
  • did change the single object to an Array but it gave me an error that says Objektreference has not been given to an instance of a object' –  Jan 07 '20 at 14:22

3 Answers3

3

Use Newtonsoft.Json

public partial class RootObject
{
    [JsonProperty("itemDetails")]
    public ItemDetail[] ItemDetails { get; set; }

    [JsonProperty("purchaserEmail")]
    public string PurchaserEmail { get; set; }

    [JsonProperty("purchaserName")]
    public string PurchaserName { get; set; }

    [JsonProperty("recipientDetails")]
    public RecipientDetails RecipientDetails { get; set; }

    [JsonProperty("disableAllEmails")]
    public object DisableAllEmails { get; set; }

    [JsonProperty("orderDate")]
    public object OrderDate { get; set; }
}

public partial class ItemDetail
{
    [JsonProperty("quantity")]
    public long Quantity { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("description")]
    public object Description { get; set; }

    [JsonProperty("code")]
    public object Code { get; set; }

    [JsonProperty("price")]
    public double Price { get; set; }

    [JsonProperty("value")]
    public double Value { get; set; }

    [JsonProperty("expiresOn")]
    public object ExpiresOn { get; set; }

    [JsonProperty("expiresInMonths")]
    public object ExpiresInMonths { get; set; }

    [JsonProperty("overrideExpiry")]
    public bool OverrideExpiry { get; set; }

    [JsonProperty("sku")]
    public string Sku { get; set; }

    [JsonProperty("id")]
    public object Id { get; set; }
}

public partial class RecipientDetails
{
    [JsonProperty("recipientName")]
    public string RecipientName { get; set; }

    [JsonProperty("recipientEmail")]
    public string RecipientEmail { get; set; }

    [JsonProperty("message")]
    public string Message { get; set; }

    [JsonProperty("scheduledFor")]
    public DateTimeOffset ScheduledFor { get; set; }
}

Deserialize it using

var Data = JsonConvert.DeserializeObject<RootObject>(json);
Sushant Yelpale
  • 860
  • 6
  • 19
0

Mark all your properties as public. default scope level is Internal and for serialization and deserialization you need public properties.

Also, if you are using Newtonsoft.Json, since json property casing is camelCase, please use CamelCasePropertyNamesContractResolver like below:

JsonConvert.DeserializeObject<Order>(json_string, new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()})

Instead, you can define JSON Settings at the beginning of the app and avoid passing it via DeserializeObject everywhere in your app.

Or you can decorate each property using JsonProperty like mentioned by Sushant Yelpale here.

Community
  • 1
  • 1
sam
  • 1,937
  • 1
  • 8
  • 14
0

If you want to serialize private fields for some reasons, I suggest this Post. Otherwise the answer from Sushant Yelpale is the right one, note that you don't especially need these [JsonProperty("disableAllEmails")] tags if the name in the Json and in your class match (and it's not case sensitive).

Platypus
  • 321
  • 1
  • 4
  • 17
  • 1
    Json is very much case sensitive – Jawad Jan 07 '20 at 14:07
  • I can ensure you that if you have a json with a "date" you Json .net can easily convert it to a `DateTime Date {get; set;}`, at least to deserialize – Platypus Jan 07 '20 at 14:14
  • I am using ReadAsAsync to deserialize it is there anyway to keep on using this? –  Jan 07 '20 at 14:14
  • I'm not sure of what you're trying to do, but i guess if you `await` the `Task` returned by ReadAsAsync it should be ok. I guess you just need to be sure to give the data in one block to Json.net. – Platypus Jan 07 '20 at 14:19