无效的 JSON 响应 MVC API



我有一个JSON请求,如下所示:

{
"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; }
}

我的数据控制器 :

[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);
}

根据上面的类和数据控制器,我返回以下 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
}
]

目前,我正在按照上述方式返回产品类别及其物品。

那么,现在如何在数据控制器中传递根对象以根据 JSON 请求获取 JSON 响应。

List<Product> prodata = new List<Product>();
RootObject _rootObject = new RootObject();
var list = db.productGetData().ToList();
foreach (var listdata in list)
{
var _listOfProperties = new List<Property>();
//if product list contains data of properties
foreach(var _prop in listdata.Properies)
{
var property = new Property();
property.PropertyKey = _prop.PropertyKey;
property.PropertyValue = _prop.PropertyValue;
_listOfProperties.Add(property);
}
Product pdata = new Product();
pdata.Properties = _listOfProperties;
prodata.Add(pdata);
}
_rootObject.Products = prodata;
and now serialize it
string stringData = JsonConvert.SerializeObject(_rootObject);

最新更新