2015-07-20 59 views
0

我的代码如下:如何在.net webservice中显示数组内的JSON输出?

[{"EmployeePhoto":"1009-employee.jpg","EmployeeID":"1009","EmpName":"JOHN PATEL","EmployeementDate":"01 Aug 2003","EDateOfBirth":"02 Jan 1979","StateName":"Gujarat","BranchName":"Maintenance","CategoryName":"Staff","SubCatName":"Technical","DepartmentName":"Electrical and Electronics","DesignationName":"MANAGER (MAINT.)","PaycaderName":"M4","EProbation":"0","ECompanyEmail":"[email protected]"}] 

但我想产生这样的输出:

{ 
    "address": { 
    "streetAddress": "21 2nd Street", 
    "city": "New York" 
    }, 
    "phoneNumber": [ 
    { 
     "location": "home", 
     "code": 44 
    } 
    ] 
} 

阵列

public class WS_Login : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public void Login(string userName, string password) 
    { 
     DAL.dbOperation dboperation = new DAL.dbOperation(); 
     dboperation.AddParameter("@EUserID", userName); 
     dboperation.AddParameter("@CompID", Convert.ToString(balUserlogin.m_Ds.Tables[0].Rows[0]["CompanyID"])); 
     DataSet ds = dboperation.getDataSet("Mobile_sp_tbl_Employee_Select_Fullinfo"); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      string[] selectedColumns = new[] { "EmployeePhoto", "EmployeeID", "EmpName", "EmployeementDate", "EDateOfBirth", "StateName", "BranchName", "CategoryName", "SubCatName", "DepartmentName", "DesignationName", "PaycaderName", "EProbation", "ECompanyEmail" }; 
      DataTable dt = new DataView(ds.Tables[0]).ToTable(false, selectedColumns); 

      var list = new List<Dictionary<string, object>>(); 

      foreach (DataRow row in dt.Rows) 
      { 

       var dict = new Dictionary<string, object>(); 

       foreach (DataColumn col in dt.Columns) 
       { 
        row[col] = row[col].ToString(); 
        dict[col.ColumnName] = row[col]; 
       } 
       list.Add(dict); 
      } 
      HttpContext.Current.Response.Write(new JavaScriptSerializer().Serialize(list)); 
     } 
    } 
} 

}这样

据生成输出内部阵列。所以我怎样才能产生这样的输出使用var list = new List>(); NET中的 webservice

回答

0

在你的例子中没有数组里面的数组。大括号是结构。对于数组,JSON使用方括号。如果你想要结构内部结构,只需定义包含另一个类的类。如果你真的想要数组在内部,定义包含数组的类。

public class A 
{ 
    public int aa { get; set; } 
    public int ab { get; set; } 
} 


public class B 
{ 
    public int ba { get; set; } 
    public A bb { get; set; } 
} 

这将编码成“结构中的结构”。而这

public class A 
{ 
    public int aa { get; set; } 
    public int ab { get; set; } 
} 


public class B 
{ 
    public int ba { get; set; } 
    public A[] bb { get; set; } 
} 

会给你“数组里面的结构”。

public class C 
{ 
    public int ca { get; set; } 
    public B[] cb { get; set; } 
} 

最后这个会给你“数组里面的数组结构”。