Decode javascript-encoded string in c#

Asked

Viewed 92 times

0

Hey, here’s the deal. On the site that I’m building, I write information in the database, this information is a json, to be able to send this data to the database, I code it by javascript like this:

encodeURI(dados);

After that I send via jquery and write to the database..

So far all beauty, only that I need to recover this data in a windows Forms software, in c#, I am looking for a way to decode this information, I tried that:

using System;
using System.Net;

...
Conecto ao banco de dados, pego a informação do jeito que está la em um string, e peço pra mostrar numa show box
MessageBox.Show(WebUtility.HtmlDecode(pedidos));

Except that it is not decoding, the result is still coming back the same is in the database, all encoded as if it were a URI

%5B%0A%09%7B%0A%09%09%22id%22%3A%221%22%2C%0A%09%09%22produto%22%3A%22VENEZA%22%2C%0A%09%09%22preco%22%3A%2242.00%22%2C%0A%09%09%22quantidade%22%3A1%0A%09%7D%0A%5D

1 answer

3


Try to use the HttpUtility.UrlDecode library System.Web

String teste = "%5B%0A%09%7B%0A%09%09%22id%22%3A%221%22%2C%0A%09%09%22produto%22%3A%22VENEZA%22%2C%0A%09%09%22preco%22%3A%2242.00%22%2C%0A%09%09%22quantidade%22%3A1%0A%09%7D%0A%5D";

Console.WriteLine(HttpUtility.UrlDecode(teste));

Return:

[
    {
        "id":"1",
        "produto":"VENEZA",
        "preco":"42.00",
        "quantidade":1
    }
]

Recommended reading: What is the difference between: Urlencode, Escapeuristring and Escapedatastring

  • Then I saw several websites teaching this there, but I import the System.Web library, but this Httputility function does not appear as if it did not exist

  • Error when importing library?

  • Ah, I get it now, I need to add it as a reference..

  • That, in case it doesn’t work out, try: Uri.UnescapeDataString

  • They both worked, thank you

  • If the answer helped you mark as accepted to help others who have the same question! :)

Show 1 more comment

Browser other questions tagged

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