سؤال

I have following method and i need to deserialize the retrieve stream.

  public void RedirectHyperlink(System.IO.Stream jsonString)
  {
       string val= JsonSteamToString(jsonString); 
  }

 public string JsonSteamToString(Stream jsonStream)
  {
       StreamReader reader = new StreamReader(jsonStream);
       return reader.ReadToEnd();
  }

My class is as follows:

 public class H2JsonStateObject
    {
        public string url { get; set; }
        public string stateId { get; set; }
    }

I'm calling this method using Ajax call as follows:

var value ="89aafdec-0a9e-4d05-b04e-1ca4bf8cfeb9";
var link="RedirectPage.aspx";
var data = '{"url":"' + link + '","stateId":"' + value + '"}';
var jsonToSend = JSON.stringify(data);

$.ajax({
    cache: false,
    url: "StateRedirectService.svc/RefreshPagemethod",
    type: "POST",
    async: false,
    data: jsonToSend,
    success: function (data, textStatus, jqXHR) {
        window.location.href = link;         
    },
    error: function (xhr, status, error) {
        alert('error');
    }
});

When the request receive to web method and after converting the value it get following string.

"\"{\\\"url\\\":\\\"RedirectPage.aspx\\\",\\\"stateId\\\":\\\"89aafdec-0a9e-4d05-b04e-1ca4bf8cfeb9\\\"}\"" 

Now i need to deserialize url & stateId. How can i do that?

I tried followings. but couldn't able to deserialize.

Using DataContractJsonSerializer

 using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(val)))
 {
       DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(H2JsonStateObject));
       H2JsonStateObject p2 = (H2JsonStateObject)deserializer.ReadObject(ms);
 }

It throws exception and says : Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''.

Using JavaScriptSerializer

  var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
  Object obj = serializer.DeserializeObject(val);

This gives me my object as string value as follows:

"{\"url\":\"RedirectPage.aspx\",\"stateId\":\"89aafdec-0a9e-4d05-b04e-1ca4bf8cfeb9\"}"

What I did wrong and how can i get RedirectPage.aspx and 89aafdec-0a9e-4d05-b04e-1ca4bf8cfeb9 value?

هل كانت مفيدة؟

المحلول

Able to solve this problem in following way. my problem is with Json string value.

 public void RedirectHyperlink(System.IO.Stream jsonString)
  {
       string val = JsonSteamToString(jsonString);         
       string JsonVal = val.Replace("\"", "'");
       var json_serializer = new JavaScriptSerializer();
       H2JsonStateObject myobj = json_serializer.Deserialize<H2JsonStateObject>(JsonVal);
       H2RequestRedirect.RedirectToTemp(myobj.url, myobj.stateId);   
  }

in here string needs to be in following format.

 JsonVal = "{'url': 'RedirectPage.aspx','stateId': '89aafdec-0a9e-4d05-b04e-1ca4bf8cfeb9' }"; 

When i'm calling this method i send json object without JSON.stringify(data);

  var data = '{"url":"' + link + '","stateId":"' + value + '"}';  
    $.ajax({
        cache: false,
        url: "StateRedirectService.svc/RedirectHyperlink",
        type: "POST",
        async: false,
        data: data,
        success: function (data, textStatus, jqXHR) {
            //alert('OK');
             window.location.href = link;        },
        error: function (xhr, status, error) {
            alert('error');
        }
    });

Then it works as a charm...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top