Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用newtonsoft将对象转换为json,但不获取所需格式的输出_C#_Json - Fatal编程技术网

C# 使用newtonsoft将对象转换为json,但不获取所需格式的输出

C# 使用newtonsoft将对象转换为json,但不获取所需格式的输出,c#,json,C#,Json,我创建了两个类 BingMapsProduct.cs 接触器.cs 但我希望最终得到产品列表数组 "AgreementType": "QC", "TotalCount": 0, "productList": [ { "ProductId": 16081, "ProductName": "Appliance Dishwasher RE/RE + Haulaway - HOME DEPOT", "ProductPrice": 95.0, "ContractorS

我创建了两个类

BingMapsProduct.cs

接触器.cs

但我希望最终得到产品列表数组

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

因为我必须将上面的json传递给bing maps api,它将重新生成格式化的json

使用属性上的
JsonProperty
属性

[JsonProperty(Order = xx)]
见:

        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"
  }
]
[JsonProperty(Order = xx)]