2013-04-18 5 views
7

deserialize नहीं होगा मैं एक json प्रतिक्रिया वापस वस्तुओं में चारsquare से बदलने की कोशिश कर रहा हूँ। मैं कुछ इस तरह वापसमुझे क्या याद आ रही है? RestSharp जेसन

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

मिल रहा फिर भी Venues इस

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; 

की तरह एक वर्ग बनाया हमेशा रिक्त है। मुझे यकीन नहीं है कि क्यों।

+0

chobo2, आपका जेसन अवैध है और फ़ील्ड के बीच कई कॉमा गायब है। तो RetSharp सही है। – I4V

+0

मान लें कि जेसन मान्य है। यह चौकोर से आ रहा है (इसे काम करना चाहिए ....)। जिस समस्या का आप वर्णन कर रहे हैं वह इसलिए था क्योंकि मैंने इसे अपने लाइव टूल से कॉपी किया जो स्पष्ट रूप से उद्धरणों को स्ट्रिप्स करता है ... कोड के माध्यम से अनुरोध उद्धरण है। – chobo2

उत्तर

17

ठीक है मुझे यह अच्छा टूल मिला http://json2csharp.com/ जो जेसन को ऑब्जेक्ट्स में परिवर्तित करता है। मुझे इसके माध्यम से पता चला कि मुझे इसे काम करने के लिए एक और लपेटना चाहिए।

+11

या विजुअल स्टूडियो में आप 'संपादित करें-> विशेष चिपकाएं-> जेसन' के रूप में क्लिक कर सकते हैं और यह आपके लिए – cheesemacfly

0

मैं सही दिशा में तो जा रहा हूँ, तो उर JSON नहीं है Valid

Error:Strings should be wrapped in double quotes 

यह मान्य हो जाओ jsonformatter

[अपडेट]

मान्य JSON होगा जैसे: -

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

सही लेकिन यह मान्य जेसन है। यह था कि मैंने अपने ऑनलाइन उपकरण से परिणाम लिया जो जेसन वापस लौटाता है (https://developer.foursquare.com/docs/explore#req=venues/search%3Fll%3D40.7,-74%26groceryStoreId%3D4bf58dd8d48988d118951735) उद्धरण दूर करने के लिए क्या लगता है।मैं अपने कोड में जो वापस प्राप्त कर रहा हूं उसके साथ पुनः पोस्ट किया गया। – chobo2

0

.NET ऑब्जेक्ट्स के लिए JSON deserialization केस संवेदनात्मक है। आपके प्रॉपर्टी नाम JSON टैग से ठीक से मेल नहीं खाते हैं, और यही कारण है कि जब आप deserialize करने का प्रयास करते हैं, तो आप वापस वापस आ रहे हैं।

1

आपको बस JSON प्रतिक्रिया में एक स्तर को गहराई से जाना होगा। venues संपत्ति से एक स्तर ऊपर response संपत्ति है, जो वर्तमान में आपके Response कक्षा में प्रदर्शित नहीं है।

आपके पास इसे हल करने के दो तरीके हैं।

1) एक और रैपिंग प्रतिक्रिया वस्तु है, जो लापता response संपत्ति

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

और अनुरोध निष्पादित होता है ...

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

2 जोड़े) meta संपत्ति पर ध्यान न दें और कम से पार्स करने शुरू response संपत्ति।

* एक तरफ, ऐसा लगता है कि JSON प्रतिक्रिया की meta संपत्ति HTTP स्थिति कोड हो सकती है। यदि यह है और आपको अभी भी इसकी आवश्यकता है, तो RestSharp आपके लिए भी प्रदान करता है (नीचे देखें)।

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

करता है यह उत्तर ओपी के मूल मुद्दे को संबोधित करता है। – wlf

संबंधित मुद्दे