0

エンティティがあります。以下に示すデータベースを使用してエンティティを edmx ファイルとしてセットアップします。edmunds.com からプルしている JSon ファイルもあります。JSon.net を使用して、JSon オブジェクトをエンティティに解析し、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 オブジェクト (つまり、{"name":"value"}) を型 'System.Data.Objects.DataClasses.EntityCollection`1[DealerTentSaleMVC.Models.Operation]' に逆シリアル化できませんという最新のエラーを次に示します。逆シリアル化された型は、通常の .NET 型 (つまり、整数のようなプリミティブ型ではなく、配列やリストのようなコレクション型ではない) またはディクショナリ型 (つまり、ディクショナリ) である必要があります。JSON オブジェクトを強制的に逆シリアル化するには、JsonObjectAttribute を型に追加します。パス 'operations.Wednesday'、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

                                                            });

具体的には、最後の行で発生しています

4

1 に答える 1

1

逆シリアル化しようとしている JSON を反映するオブジェクト構造を構築できます。

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

その後:

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
于 2012-05-24T16:28:28.440 に答える