0

我创建了两个类

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;

接触器.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 传递给需要格式化 json 的 bing maps api

4

1 回答 1

0

使用JsonProperty属性上的属性。

[JsonProperty(Order = xx)]

请参阅: http://james.newtonking.com/projects/json/help/index.html?topic=html/ T_Newtonsoft_Json_Serialization_JsonProperty.htm

于 2013-06-20T09:56:15.453 回答