JSON数组格式的数据库结果使用c#

本文关键字:结果 数据库 数组 格式 JSON | 更新日期: 2023-09-27 17:49:00

我创建了新的WCF REST服务,但我的结果与出键(不是JSON数组格式)我的结果像这样

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

我想创建一个名为Info的数组,其中每个元素包含这三个值。

"Info": 
[
    {
       "email": "abc@gmail.com",
       "name": "John",
       "mobile": "009854"
    },
    {
       "email": "safft@gmail.com",
       "name": "Smith",
       "mobile": "009114"
    }
]
我的c#代码
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;

    }
有谁能帮我吗?

显示"对象引用未设置为对象的实例"这一行的错误。

  infoContract.Info.Add(order );

我的代码是这样的

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;

}

和我的接口是

 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; }
 }

JSON数组格式的数据库结果使用c#

必须返回一个Array。

你的方法只返回一个对象,你可以尝试类似下面给出的东西。

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;}
}