Convert JSON data format using JsonConvert (object to JSON)

Source: Internet
Author: User

 

Today I learned a bit about. NET WCF components, the side of the whim, thinking now is not both back and forth, the interface development is not, so hurriedly wrote a simple background data, haha nonsense not much to say, directly on the code;

Note that you need to import the library!

Entity class: Customer

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacedomain{ Public classCustomer { Public stringCustomerId {Set;Get; }  Public stringCompanyName {Set;Get; }  Public stringContactName {Set;Get; }  Public stringAddress {Set;Get; }  Public stringtest1 {Set;Get; } };}
View Code

WCF interfaces

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text;namespaceInterface Test Learning 1{//Note: Using the rename command on the Refactor menu, you can change the interface name "IService1" in code and configuration files at the same time. [ServiceContract] Public InterfaceIService1 {[OperationContract]stringGetData (intvalue);        [OperationContract]        Compositetype getdatausingdatacontract (Compositetype composite); //TODO: Add your service actions here[OperationContract]stringGetdatajson (stringcustomer); }    //use the data contract described in the following example to add a composite type to a service operation. [DataContract] Public classCompositetype {BOOLBoolvalue =true; stringStringValue ="Hello"; [DataMember] Public BOOLBoolvalue {Get{returnBoolvalue;} Set{Boolvalue =value;} } [DataMember] Public stringStringValue {Get{returnStringValue;} Set{stringvalue =value;} }          }}
View Code

Implementation classes for WCF interfaces

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.ServiceModel.Web;usingSystem.Text;usingSystem.Data.SqlClient;usingdomain;usingNewtonsoft.json;namespaceInterface Test Learning 1{//Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in Code, SVC, and configuration files at the same time. //Note: In order to start the WCF test client to test this service, select Service1.svc or Service1.svc.cs in Solution Explorer and start debugging.      Public classService1:iservice1 { Public stringGetData (intvalue) {            return string. Format ("You entered: {0}", value); }         Publiccompositetype getdatausingdatacontract (Compositetype composite) {if(Composite = =NULL)            {                Throw NewArgumentNullException ("Composite"); }            if(composite. Boolvalue) {composite. StringValue+="Suffix"; }            returnComposite; }         Public stringGetdatajson (stringcustomer) {            stringStrcon ="Data source=192.168.99.28;initial catalog=edu; User Id=pos; Password=pos;"; SqlConnection Con=NewSqlConnection (Strcon); List<Customer> list =NewList<customer>(); Try{con.                Open (); //The SQL that will be executed//String sql = "SELECT * from Customer";                stringsql =string. Format ("select * FROM {0}", customer); //creates a command object that specifies the SQL statement to execute with the connection object ConnSqlCommand cmd =NewSqlCommand (sql, con); Console.WriteLine ("Open success, status"+con.                State); //executing a query returns a result setSqlDataReader SDR =cmd.                ExecuteReader ();  while(SDR). Read ()) {Customer C=NewCustomer (); C.customerid= sdr["CustomerId"].                    ToString (); C.companyname= sdr["CompanyName"].                    ToString (); C.contactname= sdr["ContactName"].                    ToString (); C.address= sdr["Address"].                    ToString (); C.test1= sdr["test1"].                    ToString (); List.                ADD (c); }            } Catch(Exception ex) {Console.WriteLine (ex).            Message); }            finally {            //Close Connectioncon.           Close (); //Console.WriteLine ("Total Query" + count + "bar data"); //Console.readkey ();Console.WriteLine (list.            capacity); }            //then serialize the list collection and go to the JSON            stringJSON =jsonconvert.serializeobject (list);            Console.WriteLine (JSON);            Console.readkey (); returnJSON; }                        }            }
View Code

Convert JSON data format using JsonConvert (object to 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.