JSON-related code

Source: Internet
Author: User

string @"                   "                    id" ":" "1" "," "                    title" ":" "Messagetitle1" ""                    Content "": "" HelloWorld "", "                    " "" "" "" "" Web.abc ""                  },                  {"                    id" ":" "2" ","                    "title" ":" "Messagetitle2" ","                    "Content" ":                    " "" "" "" URL "": "" http://web.abc/portal/hldjh/201404/t20140423_73322.htm ""                  }                ]              }            }";
JSON String
    /// <summary>      ///DataTable conversion to list collection/// </summary>      /// <typeparam name= "TResult" >type</typeparam>      /// <param name= "DT" >DataTable</param>      /// <returns></returns>       Public StaticList<tresult> tolist<tresult> (DataTable DT)whereTResult:class,New()    {        //Create a list of propertiesList<propertyinfo> prlist =NewList<propertyinfo>(); //Gets the entry of the TResult type instance reflectionType T =typeof(TResult); //get all the public properties of TResult and find the same property (PropertyInfo) for the TResult property and the column name of the DataTable and add to the property listArray.foreach<propertyinfo> (T.getproperties (), p = = {if(dt. Columns.indexof (p.name)! =-1) prlist. ADD (P);        }); //Create a returned collectionList<tresult> oblist =NewList<tresult>(); foreach(DataRow rowinchdt. Rows) {//Create an instance of TResultTResult OB =NewTResult (); //find the corresponding data and assign a valuePrlist. ForEach (p = = {if(Row[p.name]! = DBNull.Value) p.setvalue (ob, Row[p.name],NULL);            }); //Into the returned collection. oblist.        ADD (OB); }        returnoblist; }
datatable->list
varresult = fromAccountinchdb. Publicaccount.tolist ()whereAccount . Name.contains (keyword)SelectAccount ; Result=result.            ToList (); Jobject JSON=NewJobject (NewJproperty ("accounts",                    NewJarray (//Account Arrayresult. Select (R=NewJobject (//Account Object                            NewJproperty ("ID", r.id),NewJproperty ("name", R.name),NewJproperty ("desc", R.desc)) )                                )                                )                                );
Linq JSON

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.