Reputation: 2505
I am writing results i return from stored procedure into json. I did it but i am not sure if there is a simpler cleaner way other than what i have. Also how can i check that it really wrote to json?
var results = mySvc.GetInfo("Environment");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Id");
jsonWriter.WriteValue(results.Id);
jsonWriter.WritePropertyName("Head");
jsonWriter.WriteValue(results.Head);
jsonWriter.WritePropertyName("Value");
jsonWriter.WriteValue(results.Value);
jsonWriter.WritePropertyName("GuidTwo");
jsonWriter.WriteValue(results.GuidTwo);
jsonWriter.WritePropertyName("GuidOne");
jsonWriter.WriteValue(results.Guid1);
jsonWriter.WriteEndObject();
return results;
Upvotes: 2
Views: 147
Reputation: 16393
Install Json.NET and you simply do this:
var results = mySvc.GetInfo("Environment");
var json = JsonConvert.SerializeObject(results);
The inverse:
var results = JsonConvert.DeserializeObject<Result>(json);
Upvotes: 4
Reputation: 14453
By using ServiceStack json serializer(it's faster than the json.net)
Install-Package ServiceStack.Text
using ServiceStack.Text;
var json = results.ToJson();
Upvotes: 2
Reputation: 8726
try this
var results = mySvc.GetInfo("Environment");
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObj = js.Serialize(results);
Upvotes: 2