JSON conversions

2

I am a beginner in C # and ASP.NET, and I have doubts about JSON and dictionaries

I have a serialized dictionary that has been stored in the database with:

var json = new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(filtros.ToDictionary(item => item.Key.ToString(),
                                item =>     item.Value.ToString()));

After serializing this dictionary it is stored in the database, because this information I will use later in a view (PopUp in asp.net).

My difficulty is to deserialize these values that are in the database to put them back into a dictionary. I tried:

Code that you used to retrieve the information in the database:

<asp:Literal ID="lblFiltros" runat="server" 
Text='<%# DataBinder.Eval(Container.DataItem, "FiltroJson")%> ' />

The only way you could collect the information in the code behind was:

object filtros = new System.Web.Script.Serialization.JavaScriptSerializer()
.DeserializeObject(((Literal)e.Item.FindControl("lblFiltros")).Text);

However, I can not use it with iterations and it's not even a Dictionary. With Dictionary I used the following code snippet:

System.Collections.Generic.Dictionary<string, string> filtros = new 
System.Collections.Generic.Dictionary<string, string>();

That is, when I need .Add to chave and valor in my Dictionary I not only have a large string which is the value I get in Literal :

 ((Literal)e.Item.FindControl("lblFiltros")).Text

How can I do this conversion in the right way? Thank you.

    
asked by anonymous 05.09.2016 / 16:19

1 answer

0

Resolved:

I used the Json.NET framework applied by Nugget in Visual Studio 2015 and I imported it into the project:

using Newtonsoft.Json;

I received the values from .aspx:

string serializada = @"" + ((Literal)e.Item.FindControl("lblFiltros")).Text

And to deserialize I used the following command:

Dictionary<string, string> valores = JsonConvert.DeserializeObject<Dictionary<string, string>>(serializada);

Note: The JsonConvert method belongs to the imported package.

    
06.09.2016 / 13:22