我有一个JSON请求如下:无效JSON响应MVC API
{
"Products": [
{
"ProductId": 1,
"Barcode": "sample string 2",
"ProductExtId": "sample string 3",
"CategoryName": "sample string 4",
"BrandName": "sample string 5",
"StyleName": "sample string 6",
"ProductName": "sample string 7",
"Properies": [
{
"PropertyKey": "sample string 1",
"PropertyValue": "sample string 2"
},
{
"PropertyKey": "sample string 1",
"PropertyValue": "sample string 2"
}
]
}
]
}
按照上述JSON请求我的类,如下:
public class Propery
{
public string PropertyKey { get; set; }
public string PropertyValue { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Barcode { get; set; }
public string ProductExtId { get; set; }
public string CategoryName { get; set; }
public string BrandName { get; set; }
public string StyleName { get; set; }
public string ProductName { get; set; }
public List<Propery> Properies { get; set; }
}
public class RootObject
{
public List<Product> Products { get; set; }
//public Product Products { get; set; }
}
我DataController类:
[HttpGet]
[ResponseType(typeof(List<productGetData_Result>))]
public async Task<IHttpActionResult> GetProductData()
{
List<Product> prodata = new List<Product>();
var list = db.productGetData().ToList();
foreach (var listdata in list)
{
Product pdata = new Product();
pdata.Barcode = listdata.StockNo;
pdata.ProductExtId = "";
pdata.CategoryName = listdata.Class2Descr;
pdata.BrandName = listdata.Class1Descr;
pdata.StyleName = listdata.SubClass1Cd;
pdata.ProductName = listdata.AnalCode2;
pdata.ProductSubTitle = listdata.AnalCode2;
pdata.ProductImage = listdata.ImageID;
pdata.Color = listdata.SubClass2Cd;
pdata.ColorImage = "";
pdata.FabricName = "";
pdata.SeasonName = listdata.AnalCode3;
pdata.GroupName = listdata.Class2Descr;
pdata.SearchKeywords = listdata.ItemDesc;
pdata.MRP = Convert.ToInt16(listdata.Retail_Price);
pdata.BuyingPrice = Convert.ToInt16(listdata.CurrentCost);
pdata.StockQty = Convert.ToInt16(listdata.StockQty);
pdata.AllowedQty = Convert.ToInt16(listdata.StockQty);
pdata.ProductFor = "";
pdata.AgeType = "";
pdata.AgeFrom = 1;
pdata.AgeTo = 2;
pdata.Age = "";
pdata.KeyFeatures = "";
pdata.Description = listdata.ItemDesc;
pdata.Condition = "";
pdata.Disclaimer = "";
pdata.IsGiftAvailable = true;
pdata.IsPopular = true;
pdata.IsNewArrival = true;
pdata.IsSponsored = true;
pdata.IsShippingAvailable = true;
pdata.CompanyName = "Siddharth Creation";
pdata.ProductAgeDate = Convert.ToDateTime(listdata.Dateinsert);
pdata.IsActive = true;
pdata.IntegrationFor = "Siddharth Creation";
prodata.Add(pdata);
}
using (HttpClient client = new HttpClient())
{
//HttpRequestMessage requestmsg = new HttpRequestMessage(HttpMethod.Get, "http://retailerintegration.zoomi.in");
//requestmsg.Headers.Add("token", "");
string stringData = JsonConvert.SerializeObject(prodata);
client.BaseAddress = new Uri("");
MediaTypeWithQualityHeaderValue contentType =new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
var contentData = new StringContent(stringData, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("/api/Product/ProductSave", contentData).Result;
var Message = response.Content.ReadAsStringAsync().Result;
}
return Ok(prodata);
}
AS根据上面的类和DataController,我返回JSON响应下面:
[
{
"ProductId": 0,
"Barcode": "170604658",
"ProductExtId": "",
"CategoryName": "MENS SHIRTS",
"BrandName": "KRISS",
"StyleName": "PRN",
"ProductName": "FORMAL WEAR",
"Properies": null
},
{
"ProductId": 0,
"Barcode": "170604657",
"ProductExtId": "",
"CategoryName": "MENS SHIRTS",
"BrandName": "KRISS",
"StyleName": "PRN",
"ProductName": "FORMAL WEAR",
"Properies": null
}
]
目前我退回产品类&其项目按以上。
因此,现在如何传递datacontroller中的根对象以根据JSON请求获取JSON响应。
您将需要初始化一个新的'RootObject'并设置其'Products'属性到当前的集合 - 例如'RootObject模型=新RootObject(){产品= prodata};'然后序列化,(不''prodata') –
感谢您的回复。现在我按照JSON请求获取。 –
如何在Properies数组中添加值。 –