简体   繁体   English

无法访问从 WebClient.UploadValues() 返回的 C# object 属性

[英]Can't access C# object properties returned from WebClient.UploadValues()

Sending a WebClient() request to a Web API, my API call is returning with data which I am deserialising with JsonConvert.DeserializeObject().将 WebClient() 请求发送到 Web API,我的 API 调用返回了我正在使用 JsonConvert.DeserializeObject() 反序列化的数据。

The object then has properties which I cannot seem to reference.然后 object 具有我似乎无法引用的属性。

var coursesParams = new NameValueCollection();

coursesParams.Add("grant_type", "password");
coursesParams.Add("client_id", "ContentUpload");
coursesParams.Add("client_secret", "2dfe381b4e620fff9b4fa05997e26d141d9c2c6d");
coursesParams.Add("username", "isadmin");
coursesParams.Add("password", "xxxxxxxxx");

WebClient coursesRequest = new WebClient();

coursesRequest.Encoding = Encoding.UTF8;
coursesRequest.Headers.Add("Authorization", "Bearer " + x.access_token);
coursesRequest.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
coursesRequest.Headers.Add("Accept-Language", "en-US");
coursesRequest.Headers.Add("Accept", "text/html, application/xhtml+xml, */*");
coursesRequest.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");

var coursesData = coursesRequest.UploadValues("https://xxx.yyyyyyyyyy.com/api/course/courses", "POST", coursesParams);

var y = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(coursesData));

System.Console.ReadKey();

When I pause the program and examine the y object in the debugger, it shows properties, but I cannot reference any of them.当我暂停程序并在调试器中检查y object 时,它会显示属性,但我无法引用其中任何一个。 Screen image:屏幕图像:

在此处输入图像描述

在此处输入图像描述

And, any attempt to reference (or read) these properties results in an error:并且,任何引用(或读取)这些属性的尝试都会导致错误:

y.ChildrenTokens
error CS1061: 'object' does not contain a definition for 'ChildrenTokens' and no accessible extension method 'ChildrenTokens' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
y.Count
error CS1061: 'object' does not contain a definition for 'Count' and no accessible extension method 'Count' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

What am I doing (or seeing) wrong?我在做什么(或看到)错了什么? Are these properties simply not accesssible?这些属性根本无法访问吗? Or am I referencing them incorrectly?还是我错误地引用了它们?

In C#, you can't access properties that are not defined on a type.在 C# 中,您无法访问未在类型上定义的属性。 You can see the properties in the debugger because that is the raw text input.您可以在调试器中看到属性,因为那是原始文本输入。 But since you did not specify a type to deserialize to, the DeserializeObject method is simply creating plain object instances - and object does not contain definitions for any of your properties (ChildrenTokens, Count, etc).但是由于您没有指定要反序列化的类型, DeserializeObject方法只是创建普通object实例 - 而object不包含任何属性(ChildrenTokens、Count 等)的定义。

To fix this, you should create a class with properties that match the schema of the JSON that you are parsing.要解决此问题,您应该创建一个 class,其属性与您正在解析的 JSON 的架构相匹配。 This might be a good start:这可能是一个好的开始:

public class CourseData {
    public List<Course> courses { get; set; }
    public string item_count { get; set; }
    public bool success { get; set; }
}
public class Course {
    public int course_id { get; set; }
    public string code { get; set; }
    public string course_name { get; set; }
}

Fill in the remaining properties, and then deserialize like this:填写剩余的属性,然后像这样反序列化:

// Option 1
var y = JsonConvert.DeserializeObject<CourseData>(Encoding.ASCII.GetString(coursesData));

// Option 2
CourseData y = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(coursesData));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM