Converts JSON data to a generic set (or entity)

Source: Internet
Author: User

Take a look! This method is used to transmit a JSON data set to the background on the front-end page, for example, [{'A': '', 'B':" "},...] format data, split the data in the background, and import the data to the database in batches.

# Region JSON data conversion to a generic set (or entity) /// <summary> // convert a single JSON data record to an object // </Summary> /// <typeparam name = "T"> </typeparam> /// <Param name = "str"> escape character (Format: {: '', B:''}) </param> // <returns> </returns> Private Static t converttoentity <t> (string Str) {type T = typeof (t); object OBJ = activator. createinstance (t); var properties = T. getproperties (); string M = Str. trim ('{'). trim ('}'); string [] arr = m. split (','); (INT I = 0; I <arr. count (); I ++) {for (int K = 0; k <properties. count (); k ++) {string name = arr [I]. substring (0, arr [I]. indexof (":"); object value = arr [I]. substring (ARR [I]. indexof (":") + 1); If (properties [K]. name. equals (name) {If (properties [K]. propertytype. equals (typeof (INT) {properties [K]. setvalue (OBJ, convert. toint32 (value), null);} If (properties [K]. propertytype. equals (typeof (Str ING) {properties [K]. setvalue (OBJ, convert. tostring (value), null);} If (properties [K]. propertytype. equals (typeof (long) {properties [K]. setvalue (OBJ, convert. toint64 (value), null);} If (properties [K]. propertytype. equals (typeof (decimal) {properties [K]. setvalue (OBJ, convert. todecimal (value), null);} If (properties [K]. propertytype. equals (typeof (double) {properties [K]. setvalue (OBJ, convert. Todouble (value), null);} If (properties [K]. propertytype. equals (typeof (nullable <int>) {properties [K]. setvalue (OBJ, convert. toint32 (value), null);} If (properties [K]. propertytype. equals (typeof (nullable <decimal>) {properties [K]. setvalue (OBJ, convert. todecimal (value), null);} If (properties [K]. propertytype. equals (typeof (nullable <long>) {properties [K]. setvalue (OBJ, convert. toint64 (value ), Null);} If (properties [K]. propertytype. equals (typeof (nullable <double>) {properties [K]. setvalue (OBJ, convert. todouble (value), null);} If (properties [K]. propertytype. equals (typeof (nullable <datetime>) {properties [K]. setvalue (OBJ, convert. todatetime (value), null) ;}}return (t) OBJ ;} /// <summary> /// convert multiple JSON data to generic data /// </Summary> /// <typeparam name = "T"> </typeparam> /// <Param name = "JSO Narr "> escape character (Format: [{A:'', B: ''}, {A:'', B: ''}, {:'', b: ''}]) </param> // <returns> </returns> Public static list <t> converttolist <t> (this string jsonarr) {If (! String. isnullorempty (jsonarr) & jsonarr. startswith ("[") & jsonarr. endswith ("]") {type T = typeof (t); var properties = T. getproperties (); List <t> List = new list <t> (); string recive = jsonarr. trim ('['). trim (']'). replace ("'",""). replace ("\" "," "); string [] recivearr = recive. replace ("},{","};{"). split (';'); foreach (VAR item in recivearr) {t obj = converttoentity <t> (item); list. add (OBJ);} return list;} return NULL;} # endregion

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.