Newtonsoft.Json.Jsonconvert.Serializeobject excluding @

Asked

Viewed 200 times

0

I am running Schema for my page and need to convert the information to print it in JSON. However Newtonsoft.Json.JsonConvert.SerializeObject(schema); removes "@" from "@context" and "@type".

Like the code in Asp.NET is this:

   #region schema
        var schema = new SchemaProduct
        {
            @context = "http://schema.org",
            @type = "Product",
            productID = productId.ToString(),
            description = funcoes.SanitizeHtml(produto.DescLonga, false),
            name = produto.Titulo,
            image = fotos,
            url = Request.Url.ToString(),
            offers = new SchemaOffer
            {
                @type = "Offer",
                areaServed = string.Format("{0}/{1}", produto.Localizacao, produto.Estado),
                category = string.Format("{0} > {1} > {2}", cat1.Nome, cat2.Nome, cat3.Nome),
                price = string.Format("{0:C}/{1}", produto.ValorVista, produto.Unidade)
            }
        };
        var text = "<script type='application/ld+json'>";
        text += Newtonsoft.Json.JsonConvert.SerializeObject(schema);
        text += "</script>";
        ltSchema.Text = text;
        #endregion

How it is printed:

<script type='application/ld+json'>
    {
    "productID":".....",
    "offers":
      {
       "type":"......",
       "price":".....",
       "areaServed":"....",
       "category":"....."
      },
    "context":"http://schema.org",
    "type":".....","url":".......",
    "image":["....."]
    }
</script>

Because of this generates the following complaint: Teste feito no analisador de estrutura de dados do google https://search.google.com/structured-data/testing-tool#url=

1 answer

0


After hard research, I found a solution.

Directly inside the Schema classes I added [Newtonsoft.Json.Jsonproperty("@text")], as I demonstrate in the example below:

[Newtonsoft.Json.JsonProperty("@context")]
public string @context { get; set; }

[Newtonsoft.Json.JsonProperty("@type")]
public string @type { get; set; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.