2016-08-09 6 views
5

deserialize मैं RestSharp (जो HttpClient की तरह है) फोन है और एक वेब एपीआई विधि से डेटा वापसकी त्रुटि दिखा एक इंटरफेस का एक उदाहरण नहीं बना सकता RestSharp मैन्युअल

मैं यह त्रुटि {"Cannot create an instance of an interface."}

हो रही करने के लिए है

मेरे कोड इस तरह दिखता है:

public List<Interest> GetInterests() 
{ 
    var request = new RestRequest(apiPath + "GetInterests", Method.GET) { RequestFormat = DataFormat.Json }; 
    var response = Client.Execute<List<Interest>>(request); 
    return response.Data; 
} 

मेरे response.Content नीचे दर्शाता है।

अब समस्या यह प्रतीत होती है कि डेटा प्रतिक्रिया वस्तु की सामग्री प्रॉपर्टी में JSON के रूप में है। मैं इसे वहां से मैन्युअल रूप से deserialize कर सकते हैं। लेकिन यह पहले डेटा संपत्ति में स्वचालित रूप से deserializing था, मुझे यकीन नहीं है कि अब क्या हो रहा है।

यदि मैं मैन्युअल रूप से जेसन को deserialize, यह काम करता है - लेकिन मुझे ऐसा करने की आवश्यकता क्यों है?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 

प्रतिक्रिया:

"StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 5597)"

JSON कोड

[ 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":1, 
     "InterestName":"Nursing", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":2, 
     "InterestName":"Creating/Supporting Networks of Community Partners", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":3, 
     "InterestName":"Epidemiology", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":4, 
     "InterestName":"Maternal", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":5, 
     "InterestName":"Healthcare for the Homeless", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":6, 
     "InterestName":"HIV/AIDS, STD", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":7, 
     "InterestName":"Nutrition", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":8, 
     "InterestName":"Physical Activity", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":9, 
     "InterestName":"Oral Health", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":10, 
     "InterestName":"Public Health Communication", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":11, 
     "InterestName":"Emergency Preparedness and Response", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":12, 
     "InterestName":"Public Health Policy", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":13, 
     "InterestName":"Tuberculosis", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":14, 
     "InterestName":"Teen Pregnancy Prevention", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":15, 
     "InterestName":"Vital Registration", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":16, 
     "InterestName":"Chronic Disease", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":17, 
     "InterestName":"Finance & Management", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":18, 
     "InterestName":"Quality Improvement", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":19, 
     "InterestName":"Healthy Community Design", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":20, 
     "InterestName":"Behavioral Health and Healthcare Integration", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":21, 
     "InterestName":"Rx Drug Misuse and Abuse", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":22, 
     "InterestName":"Injury Prevention", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":23, 
     "InterestName":"School Health", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":24, 
     "InterestName":"Worksite Wellness", 
     "AddDate":"2016-07-01T17:03:21.61", 
     "ModDate":"2016-07-01T17:03:21.64", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    }, 
    { 
     "InternInterests":[ 

     ], 
     "OfficeInterests":[ 

     ], 
     "InterestId":25, 
     "InterestName":"Access to Healthy Foods", 
     "AddDate":"2016-08-03T08:10:45.4", 
     "ModDate":"2016-08-03T08:10:45.4", 
     "AddUser":"System", 
     "ModUser":"System", 
     "IsDeleted":false 
    } 
] 

लौटे मैं मैन्युअल json deserialize हैं, तो यह काम करता है - लेकिन कारण है कि मैं ऐसा करने की जरूरत है ?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content);  
+1

@Ed thx yes json बुरा था –

उत्तर

7

मैं एक ही समस्या में भाग गया। मेरे मामले में मैंने एक कस्टम ऑब्जेक्ट बनाया (आपके मामले में ब्याज वस्तु है), जिसमें वस्तुओं की आईसीओलेक्शन सूची थी। इसलिए, मैं उस आईकोलेक्शन को हटा देता हूं और इसे वस्तुओं की सूची के रूप में बना देता हूं। जांच करें कि ब्याज में कोई इंटरफेस संबंधित संपत्ति है या ठोस वस्तु संपत्ति के रूप में प्रयास करने की कोशिश करें।

आंतरिक रुचि और OfficeInterests संपत्ति की जांच करें यह किसी भी प्रकार के इंटरफ़ेस संग्रह का उपयोग कर सकता है।

+0

ऐसा होता है यदि आपके पास निष्पादन विधि पर एक प्रकार की बाधा है। जैसे टी: नया() –

+1

आईन्यूमेरेबल के साथ, या मेरे मामले में एफ # में सीक - ऐसा लगता है कि JSON.net serializer अधिक क्षमाशील है और RestSharp serializer – gezzahead

+0

की तुलना में इन प्रकारों के साथ काम करता है हम अभी भी एक ही समस्या में भाग गए हैं, और यह वास्तव में समाधान था। स्पष्ट रूप से JsonSerializer का RestSharp कार्यान्वयन कुछ हद तक निष्पक्ष है और इंटरफ़ेस सूची गुणों ('IList', 'IENumerable', आदि) को संभाल नहीं सकता है। थोड़ा कष्टप्रद - जहां तक ​​मुझे पता है कि JSON.net इसे ठीक से संभालता है - लेकिन जो भी हो। कम से कम हम जानते हैं कि ऐसा होने पर क्या करना है। –

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