简体   繁体   中英

JSON Array format for DB Result using c#

I creat new WCF REST Services but my result with out key (not in JSON Array Format) my result like this

{"email": "abc@gmail.com",
"name": "John",
"mobile": "009854"}

And I want to make an array, called Info, where each element contains these three values.

"Info": 
[
    {
       "email": "abc@gmail.com",
       "name": "John",
       "mobile": "009854"
    },
    {
       "email": "safft@gmail.com",
       "name": "Smith",
       "mobile": "009114"
    }
]

my c# code

public OrderContract GetOrderDetails(string OrderID)
    {
        OrderContract order = new OrderContract();

        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ssd"].ConnectionString);
            con.Close();
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Stu where Client_id='" + OrderID + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "contacts");

            order.email= ds.Tables["contacts"].Rows[0]["email"].ToString();
            order.name = ds.Tables["contacts"].Rows[0]["name"].ToString();
            order.mobile= ds.Tables["contacts"].Rows[0]["mobile"].ToString();




        }
        catch (Exception ex)
        {
            throw new FaultException<string>
                    (ex.Message);
        }
        return order;



    }

Any one can help me?


show me error in this line "Object reference not set to an instance of an object."

  infoContract.Info.Add(order );

my code like this

public InfoContract  GetOrderDetails(string OrderID)
{
    OrderContract order = new OrderContract();

    try
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ssd"].ConnectionString);
        con.Close();
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from Stu where Client_id='" + OrderID + "'", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "contacts");

        order.email= ds.Tables["contacts"].Rows[0]["email"].ToString();
        order.name = ds.Tables["contacts"].Rows[0]["name"].ToString();
        order.mobile= ds.Tables["contacts"].Rows[0]["mobile"].ToString();


     var infoContract = new InfoContract();
     infoContract.Info.Add(order );
     return infoContract;

    }
    catch (Exception ex)
    {
        throw new FaultException<string>
                (ex.Message);
    }
    return order;



}

and my Interface is

 public interface IOrderService
{
    [OperationContract]
    [WebGet(UriTemplate = "/GetOrderDetails/{OrderID}",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    InfoContract GetOrderDetails(string OrderID);

}

my Class is

 [DataContract]
public class OrderContract
{
    [DataMember]
     public string email{ get; set; }

    [DataMember]
    public string name{ get; set; }

    [DataMember]
    public string mobile{ get; set; }


}

[DataContract]
 public class InfoContract
 {
     [DataMember]
     public List<OrderContract> Info { get; set; }
 }

You have to return an Array.

Your method returns only one object, you can try with something similar to what is given below.

public InfoContract  GetOrderDetails(string OrderID){ 

     ...

     var infoContract = new InfoContract();
     infoContract.Info.Add(order );
     return infoContract;
}

OrderContract.cs

public class OrderContract { 
    public string email {get;set;}
    public string name {get;set;}
    public string mobile{get;set;}
}

public class InfoContract {
    public List<OrderContract> Info {get;set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM