JSon.Net Json到实体 [英] JSon.Net Json to entity

查看:117
本文介绍了JSon.Net Json到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,我将使用下面看到的数据库的实体设置为edmx文件。
我也有一个JSon文件,我从edmunds.com拉$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / p>

我尝试使用的每个选项似乎都吹了我一口气。
任何人都可以为我提供一个体面的例子,说明如何完成这个,因为这正在让我疯狂。

  {
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,
纬度:27.99785,
longitude: - 82.729321
},
name:Countrywater Ford Clearwater,
logicalName:CountrysideFordofClearwater ,
type:ROOFTOP,
make:Ford,
operations:{
Wednesday:8:30 AM-9: 00 PM,
星期二:上午8:30 - 9:00 PM,
星期四:8: 上午8点至晚上9点,
星期六:8:30 AM-8:00 PM,
星期五:上午8:30至9:00 b $ b星期一:8:30 AM-9:00 PM,
星期日: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,
地址:{
street:2525 34th St N ,
apartment:,
city:圣彼得堡,
stateCode:FL,
stateName:Florida b $ bcounty:Pinellas,
country:USA,
zipcode:33713,
latitude:27.794484,
经度:- 82.679411
},
name:圣彼得堡的Autoway Ford,
logicalName:AutowayFordofStPetersburg,
type:ROOFTOP,
make:福特,
操作:{
星期三:08:30 AM-08:00 PM,
星期二:08:30 AM-08:00 PM,
星期四:08:30 AM-08:00 PM,
星期六:09:00 AM-06:00 PM,
星期五:08:30 AM-08:00 PM,
星期一:08:30 AM-08:00 PM,
星期日: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,
地址:{
street:5815 N Dale Mabry Hwy,
apartment:,
city:Tampa,
stateCode:FL,
stateName:佛罗里达州,
县:希尔斯伯勒,
国家:美国,
邮政编码:33614,
纬度:28.00016,
longitude: - 82.505206
},
name:Bill Currie Ford,
logicalName:BillCurrieFord,
type:ROOFTOP,
make:Ford,
operations:{
Wednesday:8:00 AM-9:00 PM
星期二:上午8点到晚上9点,
星期四:上午8点到晚上9点,
星期六:8 :00 AM-8:00 PM,
星期五:上午8点至晚上9点,
星期一:上午8点至晚上9点
星期日:11:00 AM-6:00 PM
},
contactinfo:{
dealer_website:http://billcurriedirect.deale rlink.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,
纬度:27.93164,
经度: - 82.730647
},
名称:沃克福特,
logicalName WalkerFord,
type:ROOFTOP,
make:Ford,
营业:{
星期三:上午8时30分至晚上8点,
星期二:上午8时30分至晚上8点,
星期四:8:30 AM-8:00 PM,
星期六:上午8:30 - 下午6:00,
星期五:8:30 AM-8 :00 PM,
星期一:8:30 AM-8:00 PM,
星期日: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对象(即{name:value})转换为类型'System.Data.Objects.DataClasses.EntityCollection`1 [DealerTentSaleMVC.Models.Operation]'。
反序列化类型应该是一个普通的.NET类型(即不是整数类型的原始类型,而不是数组或列表的集合类型)或字典类型(即字典)。
强制JSON对象反序列化将JsonObjectAttribute添加到该类型。路径操作。星期日,第22行,第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的对象结构:

  public class Result 
{
public DealerHolder [] DealerHolder {get;组;
}

public class DealerHolder
{
public string Id {get;组; }
public string LocationId {get;组; }
public Address Address {get;组; }
public string Name {get;组; }
public string LogicalName {get;组; }
public string Type {get;组; }
public string Make {get;组; }
public Operations Operations {get;组; }
public联系Contactinfo {get;组; }
public string PublishDate {get;组; }
public bool Active {get;组; }
public string SyncPublishDate {get;组;
}

public class Address
{
public string Street {get;组; }
public string Apartment {get;组; }
public string City {get;组; }
public string StateCode {get;组; }
public string StateName {get;组; }
public string县{get;组; }
public string国家{get;组; }
public string ZipCode {get;组; }
public double Latitude {get;组; }
public double Longitude {get;组; }
}

public class Operations
{
public string Wednesday {get;组; }
public string Tuesday {get;组; }
public string Thursday {get;组; }
public string Saturday {get;组; }
public string Friday {get;组; }
public string Monday {get;组; }
public string Sunday {get;组;
}

public class Contact
{
public string Dealer_website {get;组; }
public string Email_address {get;组; }
public string Phone {get;组;
}

然后:

  string json = .... go并获取JSON 
结果result = JsonConvert.DeserializeObject< Result>(json);
//做一些结果,例如枚举
//经销商持有人集合


I have an entity, I setup the entity with the database you see below as an edmx file. I also have a JSon file that I am pulling from edmunds.com I am using JSon.net and trying to parse the JSon object into the Entity and save the values of the Json to the Entity.

Every option I try to use it seems to blow up on me. Can anyone please provide me a decent example of how to accomplish this as it is driving me insane at this point.

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

Here is the most recent Error I get Cannot deserialize JSON object (i.e. {"name":"value"}) into type 'System.Data.Objects.DataClasses.EntityCollection`1[DealerTentSaleMVC.Models.Operation]'. The deserialized type should be a normal .NET type (i.e. not a primitive type like integer, not a collection type like an array or List) or a dictionary type (i.e. Dictionary). To force JSON objects to deserialize add the JsonObjectAttribute to the type. Path 'operations.Wednesday', line 22, position 19.

that error is happening with this code.

            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

                                                            });

specifically it is happening on the last line there

解决方案

You could build an object structure that reflects the JSON that you are trying to deserialize:

public class Result
{
    public DealerHolder[] DealerHolder { get; set; }
}

public class DealerHolder
{
    public string Id { get; set; }
    public string LocationId { get; set; }
    public Address Address { get; set; }
    public string Name { get; set; }
    public string LogicalName { get; set; }
    public string Type { get; set; }
    public string Make { get; set; }
    public Operations Operations { get; set; }
    public Contact Contactinfo { get; set; }
    public string PublishDate { get; set; }
    public bool Active { get; set; }
    public string SyncPublishDate { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string Apartment { get; set; }
    public string City { get; set; }
    public string StateCode { get; set; }
    public string StateName { get; set; }
    public string County { get; set; }
    public string Country { get; set; }
    public string ZipCode { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public class Operations
{
    public string Wednesday { get; set; }
    public string Tuesday { get; set; }
    public string Thursday { get; set; }
    public string Saturday { get; set; }
    public string Friday { get; set; }
    public string Monday { get; set; }
    public string Sunday { get; set; }
}

public class Contact
{
    public string Dealer_website { get; set; }
    public string Email_address { get; set; }
    public string Phone { get; set; }
}

and then:

string json = .... go and fetch the JSON
Result result = JsonConvert.DeserializeObject<Result>(json);
// do something with the result, like for example enumerating over the 
// dealer holders collection

这篇关于JSon.Net Json到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆