Create Self Constructing Objects with JavaScriptSerializer (JSON.PARSE equivalent) Create Self Constructing Objects with JavaScriptSerializer (JSON.PARSE equivalent) json json

Create Self Constructing Objects with JavaScriptSerializer (JSON.PARSE equivalent)


Why you don't use extention method

For example.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Script.Serialization;namespace Stackoverflow.Helpers{    public static class JsonHelper    {        private static JavaScriptSerializer ser = new JavaScriptSerializer();        public static T ToJSON<T>(this string js) where T : class        {            return ser.Deserialize<T>(js);        }        public static string JsonToString(this object obj)        {            return ser.Serialize(obj);        }    }}

easy to use

//Deserializestring s = "{yammerClientId = \"1\",yammerNetwork = \"2\"}";    YammerConfig data = s.ToJSON<YammerConfig>();//Serializestring de = data.JsonToString();


Can you use Newtonsoft.Json (home) to do the deserializing quite simply. It's just one line, and works way better than the built in one.

var settings = JsonConvert.DeerializeObject<YammerSettings>(json);var json     = JsonConvert.SerializeObject(yammerSettingsObject);// you can also serialized and deserialize anon objects, control formatting, // do dictinaries and lists directly, datasets, and on and on

Check out this website for more Examples

If this doesn't cover what you are looking for, can you be more specific? This is the only JSON library we use anymore, and it takes care of everything.