The transformation of the JSON string into a C # object is implemented here using the Atrribute method. Because of functional limitations, this version is only for JSON strings, such as "response": "Hello", "id": 21231513, "result": +, "msg": "OK."; Instead of a JSON array. The atrribute here is acting on properties, like the Atrribute in NHibernate, to get this property at run time by reflection which corresponds to which key in the JSON string.
namespace jsonmapper{ [AttributeUsage (Attributetargets.property, AllowMultiple = False, inherited = False)] public class Jsonfieldattribute:attribute { private string _name = String. Empty; public string Name { get {return _name;} set {_name = value;}}}}
This is followed by the core code in the conversion tool, which mainly decomposes and parses the key and value in the JSON string, and obtains the individual corresponding properties in the object and assigns values through reflection.
Namespace jsonmapper{public class Jsontoinstance {public T toinstance<t> (string json) where T:new () {dictionary<string, string> dic = new dictionary<string, string> (); string[] fields = json. Split (', '); for (int i = 0; i < fields. Length; i++) {string[] keyvalue = fields[i]. Split (': '); Dic. ADD (Filter (keyvalue[0]), filter (keyvalue[1])); } propertyinfo[] Properties = typeof (T). GetProperties (BindingFlags.Public | BindingFlags.Instance); t entity = new T (); foreach (PropertyInfo properties) {object[] Propertyattrs = property. GetCustomAttributes (FALSE); for (int i = 0; i < propertyattrs.length; i++) {Object propertyattr = Propertyattrs [i]; if (Propertyattr is Jsonfieldattribute) { Jsonfieldattribute Jsonfieldattribute = propertyattr as Jsonfieldattribute; foreach (keyvaluepair<string,string> item in dic) {if (item. Key = = jsonfieldattribute.name) {Type T = property. PropertyType; Property. SetValue (Entity, ToType (t, item. Value), NULL); Break }}}}} return entity; } private string Filter (String str) {if (! ( Str. StartsWith ("\" ") && str. EndsWith ("\" "))) {return str; } else {return str. Substring (1, str. LENGTH-2); }} public Object ToType (type type, string value) {if (type = = typeof (String)) {return value; } MethodInfo parsemethod = null; foreach (MethodInfo mi in type. GetMethods (bindingflags.static | BindingFlags.Public)) {if (MI). Name = = "Parse" && mi. GetParameters (). Length = = 1) {parsemethod = mi; Break }} if (Parsemethod = = null) {throw new ArgumentException (string. Format ("type: {0} have not Parse static method!", type)); } return Parsemethod.invoke (null, new object[] {value}); } }}
Finally, this is the code for testing.
public class Message { //{' result ': "Response": "Who is you?!", "id": 13185569, "msg": "OK."} [Jsonfield (Name = "result")] public int Result {get; set;} [Jsonfield (Name = "response")] public string Response {get; set;} [Jsonfield (Name = "id")] public int Id {get; set;} [Jsonfield (Name = "msg")] public string MSG {get; set;} }
Class program { static void Main (string[] args) { jsontoinstance util = new Jsontoinstance (); string json = "\" response\ ": \" I am a small yellow chicken with a cat sauce, "\" id\ ": 21231513,\" result\ ": 100,\" msg\ ": \" ok.\ ""; Message m = util. Toinstance<message> (JSON); } }