Problem with deserializing json with $

2

Follow json's code:

{
  "_id": {
    "$oid": "5b0f4a926724c71d945299f9"
  },
  "_class": "notificador.domain.entity.mongodb.WebhookNotification",
  "externalId": "EVE-XXXXXXXXXXXX",
  "resourceId": "TRA-XXXXXXXXXXXX",
  "accountId": "MPA-XXXXXXXXXXXX",
  "channelId": "APP-XXXXXXXXXXXX",
  "event": "TRANSFER.FAILED",
  "url": "{{url}}",
  "token": "{{token}}",
  "resource": {
    "createdAt": "2018-05-29T15:16:03.000-03",
    "amount": 0,
    "entries": [],
    "ownId": "00",
    "_links": {
      "self": {
        "href": "https://api.moip.com.br/v2/transfers/TRA-XXXXXXXXXXXX"
      }
    },
    "fee": 0,
    "cancellationDetails": {
      "cancelledBy": "BANK",
      "description": "CPF/CNPJ nao pertence ao titular da conta",
      "code": 5
    },
    "id": "TRA-XXXXXXXXXXXX",
    "transferInstrument": {
      "method": "BANK_ACCOUNT",
      "bankAccount": {
        "id": "BKA-XXXXXXXXXXXX",
        "agencyNumber": "0000",
        "holder": {
          "taxDocument": {
            "number": "{{NUMBER}}",
            "type": "CPF"
          },
          "fullname": "{{NAME}}"
        },
        "accountNumber": "000000",
        "accountCheckNumber": "0",
        "bankName": "XX",
        "type": "CHECKING",
        "bankNumber": "000"
      }
    },
    "events": [
      {
        "createdAt": "2018-05-29T15:16:03.000-03",
        "description": "Requested",
        "type": "TRANSFER.REQUESTED"
      },
      {
        "createdAt": "2018-05-30T00:00:00.000-03",
        "description": "Failed",
        "type": "TRANSFER.FAILED"
      }
    ],
    "updatedAt": "2018-05-30T00:00:00.000-03",
    "status": "FAILED"
  },
  "status": "SENT",
  "response": {
    "headers": {},
    "status": 200
  },
  "createdAt": {
    "$date": "2018-05-31T01:06:26.909Z"
  },
  "updatedAt": {
    "$date": "2018-05-31T01:06:27.231Z"
  },
  "webhookToFire": {
    "$ref": "webhook_to_fire",
    "$id": {
      "$oid": "5b0f4a926724c71d945299f8"
    }
  }
}

Follow the code to deserialize:

return JsonConvert.DeserializeObject<Root>(json);

I get error:

  

Newtonsoft.Json.JsonSerializationException: 'Additional content found   in JSON reference object. A JSON reference object should only have a   $ ref property. Path 'webhookToFire. $ Id', line 77, position 14. '

The problem is in the $ref field. How can I resolve it?

Update

Follow Classes:

public partial class Root
{
    [JsonProperty("_id")]
    public Id Id { get; set; }

    [JsonProperty("_class")]
    public string Class { get; set; }

    [JsonProperty("externalId")]
    public string ExternalId { get; set; }

    [JsonProperty("resourceId")]
    public string ResourceId { get; set; }

    [JsonProperty("accountId")]
    public string AccountId { get; set; }

    [JsonProperty("channelId")]
    public string ChannelId { get; set; }

    [JsonProperty("event")]
    public string Event { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("token")]
    public string Token { get; set; }

    [JsonProperty("resource")]
    public Resource Resource { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("response")]
    public Response Response { get; set; }

    [JsonProperty("createdAt")]
    public AtedAt CreatedAt { get; set; }

    [JsonProperty("updatedAt")]
    public UpdatedAt UpdatedAt { get; set; }

    [JsonProperty("webhookToFire")]
    public WebhookToFire WebhookToFire { get; set; }
}

public partial class Createdat
{
    [JsonProperty("$date")]
    public DateTimeOffset Date { get; set; }
}

public partial class Id
{
    [JsonProperty("$oid")]
    public string Oid { get; set; }
}

public partial class Resource
{
    [JsonProperty("createdAt")]
    public string CreatedAt { get; set; }

    [JsonProperty("amount")]
    public long Amount { get; set; }

    [JsonProperty("entries")]
    public List<object> Entries { get; set; }

    [JsonProperty("ownId")]
    public string OwnId { get; set; }

    [JsonProperty("_links")]
    public Links Links { get; set; }

    [JsonProperty("fee")]
    public long Fee { get; set; }

    [JsonProperty("cancellationDetails")]
    public CancellationDetails CancellationDetails { get; set; }

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

    [JsonProperty("transferInstrument")]
    public TransferInstrument TransferInstrument { get; set; }

    [JsonProperty("events")]
    public List<Event> Events { get; set; }

    [JsonProperty("updatedAt")]
    public string UpdatedAt { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }
}

public partial class CancellationDetails
{
    [JsonProperty("cancelledBy")]
    public string CancelledBy { get; set; }

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

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

public partial class Event
{
    [JsonProperty("createdAt")]
    public string CreatedAt { get; set; }

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

    [JsonProperty("type")]
    public string Type { get; set; }
}

public partial class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
}

public partial class Self
{
    [JsonProperty("href")]
    public Uri Href { get; set; }
}

public partial class TransferInstrument
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("bankAccount")]
    public BankAccount BankAccount { get; set; }
}

public partial class BankAccount
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("agencyNumber")]
    public string AgencyNumber { get; set; }

    [JsonProperty("holder")]
    public Holder Holder { get; set; }

    [JsonProperty("accountNumber")]
    public string AccountNumber { get; set; }

    [JsonProperty("accountCheckNumber")]
    [JsonConverter(typeof(ParseStringConverter))]
    public long AccountCheckNumber { get; set; }

    [JsonProperty("bankName")]
    public string BankName { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("bankNumber")]
    public string BankNumber { get; set; }
}

public partial class Holder
{
    [JsonProperty("taxDocument")]
    public TaxDocument TaxDocument { get; set; }

    [JsonProperty("fullname")]
    public string Fullname { get; set; }
}

public partial class TaxDocument
{
    [JsonProperty("number")]
    public string Number { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public partial class Response
{
    [JsonProperty("headers")]
    public Headers Headers { get; set; }

    [JsonProperty("status")]
    public long Status { get; set; }
}

public partial class Headers
{
}

public partial class WebhookToFire
{
    [JsonProperty("$ref")]
    public string Ref { get; set; }

    [JsonProperty("$id")]
    public Id Id { get; set; }
}
    
asked by anonymous 17.11.2018 / 01:09

1 answer

2

You need to configure the classes in their JsonProperty properties (which you can set for example the key name of the JSON ) of following :

>
public class Rootobject
{
    [JsonProperty("_id")]
    public Id Id { get; set; }

    [JsonProperty("_class")]
    public string Class { get; set; }

    [JsonProperty("externalId")]
    public string ExternalId { get; set; }

    [JsonProperty("resourceId")]
    public string ResourceId { get; set; }

    [JsonProperty("accountId")]
    public string AccountId { get; set; }

    [JsonProperty("ChannelId")]
    public string channelId { get; set; }

    [JsonProperty("_event")]
    public string Event { get; set; }

    [JsonProperty("url")]
    public string Url { get; set; }

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

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

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

    [JsonProperty("response")]
    public Response Response { get; set; }

    [JsonProperty("createdAt")]
    public Createdat CreatedAt { get; set; }

    [JsonProperty("updatedAt")]
    public Updatedat UpdatedAt { get; set; }

    [JsonProperty("webhookToFire")]
    public Webhooktofire WebhookToFire { get; set; }
}

public class Id
{
    [JsonProperty("$oid")]
    public string Oid { get; set; }
}

public class Resource
{
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }

    [JsonProperty("amount")]
    public int Amount { get; set; }

    [JsonProperty("entries")]
    public object[] Entries { get; set; }

    [JsonProperty("ownId")]
    public string OwnId { get; set; }

    [JsonProperty("_links")]
    public Links Links { get; set; }

    [JsonProperty("fee")]
    public int Fee { get; set; }

    [JsonProperty("cancellationDetails")]
    public Cancellationdetails CancellationDetails { get; set; }

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

    [JsonProperty("transferInstrument")]
    public Transferinstrument TransferInstrument { get; set; }

    [JsonProperty("events")]
    public Event[] Events { get; set; }

    [JsonProperty("UpdatedAt")]
    public DateTime updatedAt { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }
}

public class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
}

public class Self
{
    [JsonProperty("href")]
    public string Href { get; set; }
}

public class Cancellationdetails
{
    [JsonProperty("cancelledBy")]
    public string CancelledBy { get; set; }

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

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

public class Transferinstrument
{
    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("bankAccount")]
    public Bankaccount BankAccount { get; set; }
}

public class Bankaccount
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("agencyNumber")]
    public string AgencyNumber { get; set; }

    [JsonProperty("holder")]
    public Holder Holder { get; set; }

    [JsonProperty("accountNumber")]
    public string AccountNumber { get; set; }

    [JsonProperty("accountCheckNumber")]
    public string AccountCheckNumber { get; set; }

    [JsonProperty("bankName")]
    public string BankName { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("bankNumber")]
    public string BankNumber { get; set; }
}

public class Holder
{
    [JsonProperty("taxDocument")]
    public Taxdocument TaxDocument { get; set; }

    [JsonProperty("fullname")]
    public string Fullname { get; set; }
}

public class Taxdocument
{
    [JsonProperty("number")]
    public string Number { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

public class Event
{
    [JsonProperty("createdAt")]
    public DateTime CreatedAt { get; set; }

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

    [JsonProperty("type")]
    public string Type { get; set; }
}

public class Response
{
    [JsonProperty("headers")]
    public Headers Headers { get; set; }

    [JsonProperty("status")]
    public int Status { get; set; }
}

public class Headers
{
}

public class Createdat
{
    [JsonProperty("$date")]
    public DateTime Date { get; set; }
}

public class Updatedat
{
    [JsonProperty("$date")]
    public DateTime Date { get; set; }
}

public class Webhooktofire
{
    [JsonProperty("$ref")]
    public string Ref { get; set; }

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

After that also set an option in settings ( JsonSerializerSettings ) to ignore the metadata that JSON.NET

var settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

var result = JsonConvert
      .DeserializeObject<Rootobject>(System.IO.File.ReadAllText("./json1.json"), settings);

17.11.2018 / 02:52