[英]C# Accessing JSON Stream Get.response Data in Asp.net
I am very new to C#and I am trying to contact a server and received data from the server. 我是C#的新手,正尝试与服务器联系并从服务器接收数据。 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 eg "biRvqpChQZf7Cujy5CuW0PjU1R7gIp"
我想要的是仅显示SessionID,例如“ 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: 正如TheText现在的结果是:
"{\\"utLogon_response\\":{\\"SessionID\\":\\"rP99mnHAFwI840xVJMDOJpcgmE2l6z\\"}}" “{\\” utLogon_response \\ “:{\\”的SessionID \\ “:\\” rP99mnHAFwI840xVJMDOJpcgmE2l6z \\ “}}”
Here is the object "manually" from HTML: 这是HTML中的“手动”对象:
You need to deserialize
您需要反序列化
In this example RootObject should be your class : 在此示例中,RootObject应该是您的类:
*if you dont have a class with get;set; *如果您没有get; set;的课程; property, you can create it from this website : http://json2csharp.com/
属性,您可以从以下网站创建它: http : //json2csharp.com/
Just paste your json string in that website (json2csharp), and you will get all your property. 只需将json字符串粘贴到该网站(json2csharp)中,您将获得所有财产。
JavaScriptSerializer oJS = new JavaScriptSerializer();
RootObject oRootObject = new RootObject();
oRootObject = oJS.Deserialize<RootObject>(Your JSon String);
step by step 一步步
Suppose your json is : 假设您的json是:
string TheText = "{\"utLogon_response\":{\"SessionID\":\"rP99mnHAFwI840xVJMDOJpcgmE2l6z\"}}";
And these are your class based on your json : 这些是基于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 !
You need to create an object and then deserialize instead of serializing it. 您需要创建一个对象,然后反序列化而不是序列化它。 You can use JSON.net to deserialize them.
您可以使用JSON.net反序列化它们。
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 https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
您是否尝试使用字符串SessionId属性创建utLogon_response并执行以下操作:var text = Jsonconvert.DeserializeObject(responseFromServer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.