2011-03-31 26 views
26

मेरे ऐप्स में से एक में, मुझे वेबरेक्वेस्ट से प्रतिक्रिया मिल रही है। सेवा शोकहारा सेवा है और एक परिणाम के नीचे JSON प्रारूप के लिए इसी तरह वापस आ जाएगी:HttpwebResponse json प्रतिक्रिया पढ़ना, सी #

byte[] bytes = Encoding.GetEncoding(contentEncoding).GetBytes(contents.ToString()); 
    request.ContentLength = bytes.Length; 

    using (var requestStream = request.GetRequestStream()) { 

     requestStream.Write(bytes, 0, bytes.Length); 

     using (var twitpicResponse = (HttpWebResponse)request.GetResponse()) { 

      using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) { 

       //What should I do here? 

      } 

     } 

    } 

मैं प्रतिक्रिया कैसे पढ़ सकते हैं:

{ 
    "id" : "1lad07", 
    "text" : "test", 
    "url" : "http:\/\/twitpic.com\/1lacuz", 
    "width" : 220, 
    "height" : 84, 
    "size" : 8722, 
    "type" : "png", 
    "timestamp" : "Wed, 05 May 2010 16:11:48 +0000", 
    "user" : { 
     "id" : 12345, 
     "screen_name" : "twitpicuser" 
    } 
} 

और यहाँ मेरे वर्तमान कोड है? मुझे यूआरएल और उपयोगकर्ता नाम चाहिए।

+1

Restsharp को अपनी बाकी सेवा कॉल करने के लिए जांचना एक अच्छा विचार हो सकता है https://github.com/johnsheehan/RestSharp/wiki/Getting-Started आपके जीवन को 100000x ट्रैक के नीचे आसान बना देगा, और आप सेटअप कर सकते हैं इसके लिए डीकोड करने के लिए ऑब्जेक्ट मॉडल। – anthonyvscode

उत्तर

48

सबसे पहले आप यहाँ

using (var twitpicResponse = (HttpWebResponse)request.GetResponse()) { 

     using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) { 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      var objText = reader.ReadToEnd(); 
      MyObject myojb = (MyObject)js.Deserialize(objText,typeof(MyObject)); 
     } 

    } 

मैं श्रेणीबद्ध वस्तु आपके पास के साथ परीक्षण नहीं किया में फिर एक वस्तु

public class MyObject { 
    public string Id {get;set;} 
    public string Text {get;set;} 
    ... 
} 

की जरूरत है, लेकिन आप गुण आप चाहते हैं के लिए पहुँच इस देना चाहिए।

JavaScriptSerializer System.Web.Script.Serialization

+0

उत्तर के लिए धन्यवाद। ईमानदारी से, मैं इस सवाल के लिए थोड़ा आलसी था। कुछ महीने पहले, मैंने Google यूआरएल शॉर्टनर एपीआई के लिए एक रैपर बनाया है और मैंने इसके लिए System.Web.Extensions लाइब्रेरी का भी उपयोग किया है। – tugberk

+0

मैंने आपके कोड की कोशिश की है लेकिन त्रुटि "कोई अधिभार विधि नहीं है 'deserialize' take '2' तर्क" क्या मुझे कुछ याद आती है? –

+0

यह कोड 3 साल से अधिक पुराना है। अब ऐसा करने के लिए शायद बेहतर तरीके हैं। मुझे यह भी नहीं पता कि जावास्क्रिप्टसेरियलाइज़र का कौन सा संस्करण था। यह बदल सकता है? –

11

मैं RestSharp का उपयोग करेंगे - https://github.com/restsharp/RestSharp

को deserialize करने के लिए वर्ग बनाएँ:

public class MyObject { 
    public string Id { get; set; } 
    public string Text { get; set; } 
    ... 
} 

और कोड है कि वस्तु प्राप्त करने के लिए:

RestClient client = new RestClient("http://whatever.com"); 
RestRequest request = new RestRequest("path/to/object"); 
request.AddParameter("id", "123"); 

// The above code will make a request URL of 
// "http://whatever.com/path/to/object?id=123" 
// You can pick and choose what you need 

var response = client.Execute<MyObject>(request); 

MyObject obj = response.Data; 

प्रारंभ करने के लिए http://restsharp.org/ देखें ईडी।

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