0

I am very new to C#and I am trying to contact a server and received data from the server. I can successfully receive the data, but I don't know how to access the object's properties. What I want is to display only the SessionID e.g. "biRvqpChQZf7Cujy5CuW0PjU1R7gIp"

    WebRequest LoginRequest = WebRequest.Create("xxx");
    LoginRequest.Method = "GET";
    WebResponse LoginResponse = LoginRequest.GetResponse();

    Stream LoginResponseStream = LoginResponse.GetResponseStream();
    StreamReader reader = new StreamReader(LoginResponseStream);
    string responseFromServer = reader.ReadToEnd();

    string TheText = new JavaScriptSerializer().Serialize(responseFromServer);
    Label1.Text = TheText;

As is the outcome of TheText now is:

"{\"utLogon_response\":{\"SessionID\":\"rP99mnHAFwI840xVJMDOJpcgmE2l6z\"}}"

Here is the object "manually" from HTML:

enter image description here

Frank
  • 2,109
  • 7
  • 25
  • 48

3 Answers3

2

You need to deserialize

In this example RootObject should be your class :

*if you dont have a class with get;set; property, you can create it from this website : http://json2csharp.com/

Just paste your json string in that website (json2csharp), and you will get all your property.

JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(Your JSon String);

step by step

Suppose your json is :

string TheText =  "{\"utLogon_response\":{\"SessionID\":\"rP99mnHAFwI840xVJMDOJpcgmE2l6z\"}}";

And these are your class based on your json :

public class UtLogonResponse
{
    public string SessionID { get; set; }
}

public class RootObject
{
    public UtLogonResponse utLogon_response { get; set; }
}

then use this code :

   JavaScriptSerializer oJS = new JavaScriptSerializer();
   RootObject oRootObject = new RootObject();
   oRootObject = oJS.Deserialize<RootObject>(TheText);

   var yourSessionId = oRootObject.utLogon_response.SessionID; //Done !
Saurin Vala
  • 1,898
  • 1
  • 17
  • 23
  • Could you explain your answer a bit more please. I am very new to this. How would I now do "Label1.text = oRootObject.SessionID" or something like that. How do I now display the SessionID? – Frank Feb 06 '18 at 08:46
  • @Frank, I am not good with explanation, but I tried, review it, you may understand now. – Saurin Vala Feb 06 '18 at 08:59
  • I tried exactly your code but it seems to give problems. Here is a screenshot of what I have https://imgur.com/NDWzzdu – Frank Feb 06 '18 at 09:15
  • Perfect! Thank you! Maybe just replace your "TheText" with "responseFromServer" just for it to align with my question code – Frank Feb 06 '18 at 09:45
2

You need to create an object and then deserialize instead of serializing it. You can use JSON.net to deserialize them.

class Test
{
   public string SessionID { get; set; }
}

//after getting response from server
Test tmp = JsonConvert.DeserializeObject<Test>(responseFromServer);
Label1.Text = tmp.SessionID; 

Below links might help you a lot:

https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

Newtonsoft JSON Deserialize

Jayakrishnan
  • 4,232
  • 2
  • 23
  • 35
  • 1
    Even though your answer might be the best one, the lack of explanation caused me to rather focus on the other answer to find my solution :) – Frank Feb 06 '18 at 09:55
0

Did you try to create an utLogon_response with string SessionId Property and to do : var text = Jsonconvert.DeserializeObject(responseFromServer)

MrLoyal
  • 1
  • 2
  • Even though your answer might be the best one, the lack of explanation caused me to rather focus on the other answer to find my solution :) – Frank Feb 06 '18 at 09:55
  • No problem :) When i published my response, the example of my code got truncated.. don't know why! – MrLoyal Feb 06 '18 at 13:24