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

3 Answers 3

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 !
Sign up to request clarification or add additional context in comments.

4 Comments

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, I am not good with explanation, but I tried, review it, you may understand now.
I tried exactly your code but it seems to give problems. Here is a screenshot of what I have imgur.com/NDWzzdu
Perfect! Thank you! Maybe just replace your "TheText" with "responseFromServer" just for it to align with my question code
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

1 Comment

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 :)
0

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

2 Comments

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 :)
No problem :) When i published my response, the example of my code got truncated.. don't know why!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.