1
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{    
    string json= streamReader.ReadToEnd();
    List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request[json], typeof(List<DeSerialiseBL>));
}

i want to deserialize the json value.so i use the above code.when i build 'Request is not exist in the current context' error displayed.

Roman
  • 11,966
  • 10
  • 38
  • 47
Ajisha
  • 47
  • 9

3 Answers3

1

In the code in your question, you're successfully deserializing the JSON string you receive from a web request you issue through HttpWebRequest.

Say the response contains { "foo" : "bar" }, then that's the value the variable json contains.

But the expression Request[json] that follows it, makes no sense. I can assure you that the request variables do not contain a key called { "foo" : "bar" }, so the expression Request[json] returns an empty string.

You should not use Request there, but directly pass the json variable:

List<DeSerialiseBL> myDeserializedObjList = 
    (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<DeSerialiseBL>));

The code can be further simplified:

using Newtonsoft.Json; // At the top of your file

var myDeserializedObjList = JsonConvert.DeserializeObject<List<DeSerialiseBL>>(json);

So you don't need HttpRequest.Current, because the json string has no relation at all to the current request.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 – Ajisha Feb 25 '17 at 10:30
  • Yeah that's a whole nother problem: the JSON structure does not match your class `DeSerialiseBL`. Use http://json2csharp.com to create classes that match the JSON. – CodeCaster Feb 25 '17 at 10:33
  • var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { string result = streamReader.ReadToEnd(); List myDeserializedObjList = (List)Newtonsoft.Json.JsonConvert.DeserializeObject(result, typeof(List)); } – Ajisha Feb 25 '17 at 10:44
  • public class Product { public string id { get; set; } public string name { get; set; } } public class RootObject { public List products { get; set; } public int success { get; set; } } – Ajisha Feb 25 '17 at 10:48
0

try this

 string Url = "Your Url";

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(Url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.GetAsync(Url).Result;
    if (response.IsSuccessStatusCode)
    {
        var JsonResult = response.Content.ReadAsStringAsync().Result;
        System.Web.Script.Serialization.JavaScriptSerializer tmp = new System.Web.Script.Serialization.JavaScriptSerializer();
        RemoteResult r = (RemoteResult)tmp.Deserialize(JsonResult, typeof(RemoteResult));
         // r.myDeserializedObjList is your desired output
    }

and create class for your desired result.That is

public class RemoteResult
    {
        List<DeSerialiseBL> myDeserializedObjList;
    }
Prasanna Kumar J
  • 1,288
  • 3
  • 17
  • 35
0

firstly you can not use HttpContext.Current in win form. winform not running in request pipeline..

DeserializeObject waiting serialize string and type..

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {    
         string json= streamReader.ReadToEnd();
         //List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request[json], typeof(List<DeSerialiseBL>));
         List<DeSerialiseBL> myDeserializedObjList = (List<DeSerialiseBL>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<DeSerialiseBL>));
    }
levent
  • 3,464
  • 1
  • 12
  • 22
  • when i executed this code 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1' error displayed – Ajisha Feb 25 '17 at 11:09