Biseness
Biseness

Reputation: 25

Can't convert Json

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

Answers (2)

Gevorg Gevorgyan
Gevorg Gevorgyan

Reputation: 36

You can use Newtonsoft.

Steps:

  1. Download the from this link: http://json.codeplex.com/

  2. Add reference Newtonsoft.Json.dll

  3. 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

Nicolas Tyler
Nicolas Tyler

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

Related Questions