简体   繁体   中英

Convert string to JSON object c#

I've this error in my code:

Invalid object passed in, ':' or '}' expected. (14): { first_name = teste, last_name = teste, dia = 1, mes = 1, ano = 1890, mail = 1890, company = , ocupation = dsafad, pass = 123, country = Antigua, city = ffff, user_type = 40 }

I'm trying to convert this string to json, but i can't how can i do this.

var user_data = new {
   first_name = register.first_name,
   last_name = register.last_name,
   dia = register.dia,
   mes = register.mes,
   ano = register.ano,
   mail = register.ano,
   company = register.company,
   ocupation = register.ocupation,
   pass = register.pass,
   country = register.country,
   city = register.city,
   user_type = register.user_type
};
Session["JSON_OBJECT-USER-PREMIUM"] = user_data;

and i do this on the other side to convert:

string new_user = Session["JSON_OBJECT-USER-PREMIUM"].ToString();
var json = new JavaScriptSerializer();
var data = json.Deserialize<Dictionary<string, string>[]>(new_user);
Response.Write(data);

The object register itself will be enough for serialization.

Session["JSON_OBJECT-USER-PREMIUM"] = register;

// here the type Register is whatever the type of object 'register' is
Register new_user = (Register)Session["JSON_OBJECT-USER-PREMIUM"]; 

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(new_user);

Response.Write(json);

Deserialization:

var registerObject = serializer.Deserialize<Register>(json);
Response.Write(registerObject);

And with these little changes you can do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM