2013-04-18 14 views
7

Próbuję odwrócić odpowiedź jsona z foursquare na obiekty. Mam coś takiego z powrotemCzego mi brakuje? RestSharp nie deserializuje Json'a

{ 
    "meta":{ 
     "code":200 
    }, 
    "response":{ 
     "venues":[ 
     { 
      "id":"4abfb58ef964a520be9120e3", 
      "name":"Costco", 
      "contact":{ 
       "phone":"6045967435", 
       "formattedPhone":"(604) 596-7435" 
      }, 
      "location":{ 
       "address":"7423 King George Hwy", 
       "crossStreet":"btw 76 Avenue & 73A Avenue", 
       "lat":49.138259617056015, 
       "lng":-122.84723281860352, 
       "distance":19000, 
       "postalCode":"V3W 5A8", 
       "city":"Surrey", 
       "state":"BC", 
       "country":"Canada", 
       "cc":"CA" 
      }, 
      "canonicalUrl":"https:\/\/foursquare.com\/v\/costco\/4abfb58ef964a520be9120e3", 
      "categories":[ 
       { 
        "id":"4bf58dd8d48988d1f6941735", 
        "name":"Department Store", 
        "pluralName":"Department Stores", 
        "shortName":"Department Store", 
        "icon":{ 
        "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/shops\/departmentstore_", 
        "suffix":".png" 
        }, 
        "primary":true 
       } 
      ], 
      "verified":true, 
      "restricted":true, 
      "stats":{ 
       "checkinsCount":2038, 
       "usersCount":533, 
       "tipCount":12 
      }, 
      "url":"http:\/\/www.costco.ca", 
      "specials":{ 
       "count":0, 
       "items":[ 

       ] 
      }, 
      "hereNow":{ 
       "count":0, 
       "groups":[ 

       ] 
      }, 
      "referralId":"v-1366316196" 
     } 
     ] 
    } 
} 

Zrobiłem klasy jak ten

public class Response 
    { 
     public string Meta { get; set; } 
     public List<Venue> Venues { get; set; } 
    } 

    public class Venue 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
     public Contact Contact { get; set; } 
     public Location Location { get; set; } 
     public string CanonicalUrl { get; set; } 
     public Categories Categories { get; set; } 
     public bool Verified { get; set; } 
    } 

var response = client.Execute<Response>(request); 
     var test = response.Data; 

Jeszcze Venues zawsze ma wartość null. Nie jestem pewien dlaczego.

+0

chobo2, twój json jest nieprawidłowy i brakuje wielu przecinków między polami. Więc RetSharp ma rację. – I4V

+0

Załóżmy, że json jest poprawny. Pochodzi z foursquare (musi działać ...). Problem, który opisujesz, polegał na tym, że skopiowałem go ze swojego narzędzia na żywo, co najwyraźniej usuwa cytaty ... prośba za pośrednictwem kodu ma cytaty. – chobo2

Odpowiedz

17

Ok Znalazłem to wspaniałe narzędzie http://json2csharp.com/, który konwertuje json w obiekty. Przekonałem się, że potrzebuję jeszcze jednego opakowania, żeby działało.

+11

Lub w Visual Studio możesz kliknąć "Edytuj-> Wklej specjalnie-> jako Json' i zrobi to za ciebie – cheesemacfly

0

Jeżeli idę w dobrym kierunku, a następnie, ur JSON nie jest Valid

Error:Strings should be wrapped in double quotes 

Pobierz zatwierdzone jsonformatter

[AKTUALIZACJA]

Ważny JSON byłoby jak: -

{ 
"meta": { 
    "code": 200 
     }, 
    "notifications": 
     [ 
      { 
       "type": "notificationTray", 
       "item": { 
      "unreadCount": 0 
       } 
      } 
     ], 
    "response": { 
    "venues": [ 
     { 
      "id": "4e15d1348877cd5712112a44", 
      "name": "The Arena", 
    "contact": { }, 
    "location": { 
     "address": "110 Wall Street", 
     "lat": 40.70432634495503, 
     "lng": -74.0055421062419, 
     "distance": 671, 
     "city": "New York", 
     "state": "NY", 
     "country": "United States", 
     "cc": "US" 
    }, 
    "canonicalUrl": "https://foursquare.com/v/the-arena/4e15d1348877cd5712112a44", 
    "categories": [ 
     { 
      "id": "4bf58dd8d48988d1e4941735", 
      "name": "Campground", 
    "pluralName": "Campgrounds", 
    "shortName": "Campground", 
    "icon": { 
      "prefix": "https://foursquare.com/img/categories_v2/parks_outdoors/campground_", 
      "suffix": ".png" 
    }, 
    "primary": true 
} 
], 
"verified": false, 
"stats": { 
     "checkinsCount": 149, 
     "usersCount": 25, 
     "tipCount": 4 
}, 
"specials": { 
     "count": 0, 
     "items": [ ] 
}, 
"hereNow": { 
     "count": 0, 
     "groups": [ ] 
}, 
"referralId": "v-1366314443" 
}   
] 
} 

} 
+0

prawe, ale jest prawidłowe json. To było, że wziąłem wyniki z ich narzędzia online, które zwraca json z powrotem (https://developer.foursquare.com/docs/explore#req=venues/search%3Fll%3D40.7,-74%26groceryStoreId%3D4bf58dd8d48988d118951735) co wydaje się usuwać cytaty.Ponownie opublikowałem to, co otrzymuję z powrotem w moim kodzie. – chobo2

0

Odwzorowanie JSON na obiekty .NET jest wrażliwe na wielkość liter. Twoje nazwy właściwości nie pasują do znaczników JSON poprawnie, i dlatego, gdy próbujesz deserializować, otrzymujesz zwrot NULL.

1

Po prostu musisz przejść o poziom głębiej w odpowiedzi JSON. Poziom wyższy od właściwości venues to właściwość response, która obecnie nie jest reprezentowana w klasie Response.

Masz dwa sposoby rozwiązania tego problemu.

1) Dodaj kolejny obiekt odpowiedzi owijania, który zawiera brakujące response własność

// this is the new wrapping object 
public class FourSquareResponse 
{ 
    public string Meta { get; set; } 
    public VenueResponse Response { get; set; } // previously missing 
} 

public class VenueResponse 
{ 
    public List<Venue> Venues { get; set; } 
} 

public class Venue 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public Contact Contact { get; set; } 
    public Location Location { get; set; } 
    public string CanonicalUrl { get; set; } 
    public Categories Categories { get; set; } 
    public bool Verified { get; set; } 
} 

i wykonywania żądania ...

var request = new RestRequest(uri); 
var response = client.Execute<Response>(request); 

2) Ignoruj ​​własność meta i zacząć parsowania u response Właściwość.

* Na marginesie wygląda na to, że właściwość JSON może być kodem statusu HTTP. Jeśli tak jest i nadal go potrzebujesz, RestSharp zapewnia to także dla ciebie (patrz poniżej).

public class Response 
{ 
    public string Meta { get; set; } 
    public List<Venue> Venues { get; set; } 
} 

public class Venue 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public Contact Contact { get; set; } 
    public Location Location { get; set; } 
    public string CanonicalUrl { get; set; } 
    public Categories Categories { get; set; } 
    public bool Verified { get; set; } 
} 

Jednak będzie to wymagało powiadomienia RestSharp, gdzie rozpocząć analizowanie odpowiedzi.

var request = new RestRequest(uri) 
{ 
    RootElement = "response" 
}; 
var response = client.Execute<Response>(request); 

// and the HTTP status (if that's what you need) 
response.StatusCode 
+0

Ta odpowiedź odnosi się do podstawowej kwestii OP. – wlf

Powiązane problemy