2013-06-20 63 views
0

得到了我創建了兩個班轉換對象爲JSON使用newtonsoft但不是必需的fdormat

BingMapsProduct.cs

public BingMapsProduct() 
{ 
    productId = int.MinValue; 
    productName = string.Empty; 
    productPrice = double.MinValue; 
    contractorStatus = string.Empty; 
} 
public int productId; 
public string productName; 
public double productPrice; 
public string contractorStatus; 

Contactor.cs

public BingMapsContractor() 
{ 
    contactSystemType = string.Empty; 
    agreementType = string.Empty; 
    totalCount = int.MinValue; 
    productList = new List<BingMapsProduct>(); 
} 
Public string contactSystemType; 
Public string agreementType; 
Public int totalCount;  
public List<BingMapsProduct> productList; 

類的代碼,我使用的轉換

 DataSet objDataSet = GetContractorByCategory(jobId); \\returns the data from database 

     List<BingMapsContractor> contractorList = new List<BingMapsContractor>(); 

     foreach (DataRow contractorDR in objDataSet.Tables[0].Rows) 
     { 
      BingMapsContractor contractor = new BingMapsContractor(); 
      contractor.ContactSystemType =(string)contractorDR["Contact_System"]; 
      contractor.AgreementType =(string)contractorDR["agreementType"]; 
      contractor.TotalCount = (int)contractorDR["totalCount"]; 
      foreach (DataRow ProductDR in objDataSet.Tables[1].Select("contractorid = " + contractor.ContractorId)) 
      { 
       BingMapsProduct product = new BingMapsProduct(); 

       product.ProductId = (int)ProductDR["productId"]; 
       product.ProductName = Convert.ToString(ProductDR["CategoryName"]); 
       product.ProductPrice = Convert.ToDouble(ProductDR["productPrice"]); 
       product.ContractorStatus = Convert.ToString(ProductDR["contractorStatus"]); 

       contractor.productList.Add(product); 
      } 

      contractorList.Add(contractor); 
     } 

     // Return JSON data 
     string strJson = JsonConvert.SerializeObject(contractorList,Formatting.Indented); 
     return strJson; 
    } 

輸出

"productList": [ 
    { 
    "ProductId": 16081, 
    "ProductName": "Appliance Dishwasher RE/RE + Haulaway - HOME DEPOT", 
    "ProductPrice": 95.0, 
    "ContractorStatus": "Provider" 
    } 
], 
"ContactSystemType": "System", 
"AgreementType": "QC", 
"TotalCount": 0 

但我想在端的產品清單陣列。

"AgreementType": "QC", 
"TotalCount": 0, 
"productList": [ 
    { 
    "ProductId": 16081, 
    "ProductName": "Appliance Dishwasher RE/RE + Haulaway - HOME DEPOT", 
    "ProductPrice": 95.0, 
    "ContractorStatus": "Provider" 
    } 
] 

因爲我要通過上述JSON來Bing地圖API,它reuire一個格式化JSON

回答