2

Segue código json:

{
  "_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"
    }
  }
}

Segue código para desserializar:

return JsonConvert.DeserializeObject<Root>(json);

Recebo erro:

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.'

O problema está no campo $ref. Como eu posso resolver ?

Update

Segue 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; }
}
5
  • Não entendi ao certo o que você quer... Você deseja tirar o $ dos atributos do seu JSON? 17/11/2018 às 0:20
  • @EduardoRibeiro Do jeito que está no código json, não consegue desserializar. Me parace que tem que remover $. 17/11/2018 às 0:22
  • Poste a classe Root que criou
    – Syzoth
    17/11/2018 às 1:46
  • @GuilhermeNascimento ok, aguarde. 17/11/2018 às 1:46
  • Rapaz, eu estava tentando resolver em JS, até sonhei com isso a noite kkk Mas vejo que já resolveram seu problema, então fica para a próxima kkk Acho que nem iria resolver seu problema também. 17/11/2018 às 9:06

1 Resposta 1

2

Você precisa configurar na classes em suas propriedades JsonProperty (que pode definir por exemplo o nome da chave do JSON) da seguinte forma:

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; }
}   

Depois disso também configure uma opção nos settings(JsonSerializerSettings) para ignorar os metadados que o JSON.NET faz em chaves com o nome de ref trate como propriedade normal, exemplo:

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

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

Referencia:

8
  • 1
    Cara, você é muito bom nisso 👍, resolveu meu problema 17/11/2018 às 1:58
  • 1
    Eu tava tentando fazer desse jeito: PreserveReferencesHandling.None e não funcionou. 17/11/2018 às 2:02
  • 1
    Tem que ignorar ... vai aproveitando porque para versão 3 do net core isso vai mudar ... @MatheusMiranda
    – novic
    17/11/2018 às 2:03
  • 1
    Tem como configurar direto pelo [JsonProperty()] ? Acho que não neh ? 17/11/2018 às 2:03
  • 1
    @MatheusMiranda não tem essa configuração pelo visto, eu pelo menos não encontrei
    – novic
    17/11/2018 às 2:17

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .