Reputation: 25
I convert String to json. But how convert it to String?
String memb ="Hello 'test' From TestController! \r\n";
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
memb = serializer.Serialize(memb);//memb="\"\\\"Hello \\\\u0027test\\\\u0027 From Test Controller! \\\\r\\\\n\\\"\""
How convert (deserialize) to string?
P.S. It project in Console and I have problem reference library using System.Web.Extensions
Upvotes: 1
Views: 132
Reputation: 36
You can use Newtonsoft.
Steps:
Download the from this link: http://json.codeplex.com/
Add reference Newtonsoft.Json.dll
add using in the relevant file: using Newtonsoft.Json;
Then you can use it as following:
String memb = "Hello 'test' From TestController! \r\n";
string output = JsonConvert.SerializeObject(memb);
String deserializedProduct = JsonConvert.DeserializeObject<String>(output);
Upvotes: -2
Reputation: 10552
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize("string here"); //to json
string str = serializer.Deserialize(json, typeof(string)).ToString(); //to string
Upvotes: 3