我有一個實體,我用下面看到的數據庫設置實體作爲一個edmx文件。 我也有一個JSon文件,我從edmunds.com 我正在使用JSon.net,並試圖解析JSon對象到實體中,並將Json的值保存到實體。JSon.Net Json到實體
我嘗試使用它的每個選項似乎都炸燬了我。 任何人都可以請給我一個體面的例子,說明如何實現這一點,因爲它在這一點上令我瘋狂。
{
"dealerHolder":[
{
"id":"31711",
"locationId":"3730",
"address":{
"street":"24825 US Hwy 19 N",
"apartment":"",
"city":"Clearwater",
"stateCode":"FL",
"stateName":"Florida",
"county":"Pinellas",
"country":"USA",
"zipcode":"33763",
"latitude":27.99785,
"longitude":-82.729321
},
"name":"Countryside Ford of Clearwater",
"logicalName":"CountrysideFordofClearwater",
"type":"ROOFTOP",
"make":"Ford",
"operations":{
"Wednesday":"8:30 AM-9:00 PM",
"Tuesday":"8:30 AM-9:00 PM",
"Thursday":"8:30 AM-9:00 PM",
"Saturday":"8:30 AM-8:00 PM",
"Friday":"8:30 AM-9:00 PM",
"Monday":"8:30 AM-9:00 PM",
"Sunday":"11:00 AM-5:00 PM"
},
"contactinfo":{
"dealer_website":"http://www.clearwaterford.com/",
"email_address":"",
"phone":""
},
"publishDate":"2012-05-21",
"active":true,
"syncPublishDate":"2012-05-21"
},
{
"id":"31673",
"locationId":"3708",
"address":{
"street":"2525 34th St N",
"apartment":"",
"city":"Saint Petersburg",
"stateCode":"FL",
"stateName":"Florida",
"county":"Pinellas",
"country":"USA",
"zipcode":"33713",
"latitude":27.794484,
"longitude":-82.679411
},
"name":"Autoway Ford of St Petersburg",
"logicalName":"AutowayFordofStPetersburg",
"type":"ROOFTOP",
"make":"Ford",
"operations":{
"Wednesday":"08:30 AM-08:00 PM",
"Tuesday":"08:30 AM-08:00 PM",
"Thursday":"08:30 AM-08:00 PM",
"Saturday":"09:00 AM-06:00 PM",
"Friday":"08:30 AM-08:00 PM",
"Monday":"08:30 AM-08:00 PM",
"Sunday":"12:00 PM-05:00 PM"
},
"contactinfo":{
"dealer_website":"http://www.autowayford.net/",
"email_address":"",
"phone":""
},
"publishDate":"2012-05-21",
"active":true,
"syncPublishDate":"2012-05-21"
},
{
"id":"31636",
"locationId":"3684",
"address":{
"street":"5815 N Dale Mabry Hwy",
"apartment":"",
"city":"Tampa",
"stateCode":"FL",
"stateName":"Florida",
"county":"Hillsborough",
"country":"USA",
"zipcode":"33614",
"latitude":28.00016,
"longitude":-82.505206
},
"name":"Bill Currie Ford",
"logicalName":"BillCurrieFord",
"type":"ROOFTOP",
"make":"Ford",
"operations":{
"Wednesday":"8:00 AM-9:00 PM",
"Tuesday":"8:00 AM-9:00 PM",
"Thursday":"8:00 AM-9:00 PM",
"Saturday":"8:00 AM-8:00 PM",
"Friday":"8:00 AM-9:00 PM",
"Monday":"8:00 AM-9:00 PM",
"Sunday":"11:00 AM-6:00 PM"
},
"contactinfo":{
"dealer_website":"http://billcurriedirect.dealerconnection.com/",
"email_address":"",
"phone_areacode":"888",
"phone_postfix":"1156",
"phone_prefix":"255",
"phone":"8882551156"
},
"publishDate":"2012-05-21",
"active":true,
"syncPublishDate":"2012-05-21"
},
{
"id":"31723",
"locationId":"3739",
"address":{
"street":"17556 US 19 N",
"apartment":"",
"city":"Clearwater",
"stateCode":"FL",
"stateName":"Florida",
"county":"Pinellas",
"country":"USA",
"zipcode":"33764",
"latitude":27.93164,
"longitude":-82.730647
},
"name":"Walker Ford",
"logicalName":"WalkerFord",
"type":"ROOFTOP",
"make":"Ford",
"operations":{
"Wednesday":"8:30 AM-8:00 PM",
"Tuesday":"8:30 AM-8:00 PM",
"Thursday":"8:30 AM-8:00 PM",
"Saturday":"8:30 AM-6:00 PM",
"Friday":"8:30 AM-8:00 PM",
"Monday":"8:30 AM-8:00 PM",
"Sunday":"11:30 AM-6:00 PM"
},
"contactinfo":{
"dealer_website":"http://www.walkerford.com/",
"email_address":"",
"phone_areacode":"727",
"phone_postfix":"3673",
"phone_prefix":"535",
"phone":"7275353673"
},
"publishDate":"2012-05-21",
"active":true,
"syncPublishDate":"2012-05-21"
}
]
}
http://blog.redeyeproject.com/databasediagram.png
這裏是最近的錯誤,我得到 無法反序列化JSON對象(即{ 「名」: 「值」})到類型「System.Data.Objects.DataClasses.EntityCollection` 1 [DealerTentSaleMVC.Models.Operation]」。 反序列化類型應該是一個普通的.NET類型(即不是象整數這樣的原始類型,不是象數組或列表那樣的集合類型)或字典類型(即字典)。 要強制JSON對象反序列化,請將JsonObjectAttribute添加到類型中。 Path'operations.Wednesday',line 22,position 19.
該代碼發生錯誤。
WebClient wc = new WebClient();
string stws = wc.DownloadString(url);
JToken root = JObject.Parse(stws);
JToken dealerholder = root["dealerHolder"];
DealerHolder convertedProducts = new DealerHolder();
string dh = dealerholder.ToString().Replace("[", "").Replace("]", "");
JsonConvert.PopulateObject(dh, convertedProducts, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
具體地說,它是發生在最後一行有
使用您的代碼得到錯誤 無法反序列化JSON對象(即{「name」:「value」})到類型'System.Data.Objects.DataClasses.EntityCollection'1 [DealerTentSaleMVC.Models.Operation]'中。 反序列化類型應該是正常的.NET類型(即不是像整數的基本類型,而不是像數組或列表的集合類型)或字典類型(即Dictionary )。 要強制JSON對象反序列化,請將JsonObjectAttribute添加到類型中。 Path'dealerHolder [0] .operations.Wednesday',line 1,position 404。 –
scripter78