【发布时间】:2017-02-15 14:18:13
【问题描述】:
所以我使用 json2csharp 创建了一个类
public class ResponseType
{
public class Query
{
public string q { get; set; }
public object sku { get; set; }
public int limit { get; set; }
public object reference { get; set; }
public object mpn_or_sku { get; set; }
public string mpn { get; set; }
public object brand { get; set; }
public string __class__ { get; set; }
public int start { get; set; }
public object seller { get; set; }
}
public class Request
{
public bool exact_only { get; set; }
public string __class__ { get; set; }
public List<Query> queries { get; set; }
}
public class Seller
{
public string display_flag { get; set; }
public bool has_ecommerce { get; set; }
public string name { get; set; }
public string __class__ { get; set; }
public string homepage_url { get; set; }
public string id { get; set; }
public string uid { get; set; }
}
public class Prices
{
public List<List<object>> USD { get; set; }
public List<List<object>> JPY { get; set; }
public List<List<object>> CNY { get; set; }
}
public class Offer
{
public string sku { get; set; }
public string packaging { get; set; }
public string on_order_eta { get; set; }
public string last_updated { get; set; }
public int? order_multiple { get; set; }
public int in_stock_quantity { get; set; }
public string eligible_region { get; set; }
public int? moq { get; set; }
public int? on_order_quantity { get; set; }
public object octopart_rfq_url { get; set; }
public string __class__ { get; set; }
public Seller seller { get; set; }
public string product_url { get; set; }
public object factory_order_multiple { get; set; }
public string _naive_id { get; set; }
public int? factory_lead_days { get; set; }
public Prices prices { get; set; }
public bool is_authorized { get; set; }
public bool is_realtime { get; set; }
}
public class Brand
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Manufacturer
{
public string homepage_url { get; set; }
public string __class__ { get; set; }
public string name { get; set; }
public string uid { get; set; }
}
public class Item
{
public List<Offer> offers { get; set; }
public string uid { get; set; }
public string mpn { get; set; }
public List<object> redirected_uids { get; set; }
public Brand brand { get; set; }
public string octopart_url { get; set; }
public string __class__ { get; set; }
public Manufacturer manufacturer { get; set; }
}
public class Result
{
public List<Item> items { get; set; }
public int hits { get; set; }
public string __class__ { get; set; }
public object reference { get; set; }
public object error { get; set; }
}
public class RootObject
{
public int msec { get; set; }
public Request request { get; set; }
public string __class__ { get; set; }
public List<Result> results { get; set; }
}
}
问题出在设计时,当我用我的类的类型声明一个变量时:
ResponseType Response = new ResponseType();
Intellisense 不允许我访问子类 RootObject.results 列表。它只显示Equals、GetHashCode、GetType 和ToString。我假设我在班级声明中做错了什么。
提前谢谢你!
编辑——我对 C Sharp 相当陌生。我正在尝试解析来自 REST API 的响应。我获取了 Rest API 提供的 JSON,并使用 json2csharp 将其转换为一个类。我的意图是做这样的事情
在函数内返回:
public ResponseType ExecuteSearch(String PartNumber)
{
~生成req的所有代码
// Perform the search and obtain results
var data = client.Execute(req).Content;
JSON = data;
return JsonConvert.DeserializeObject<ResponseType>(data);
}
然后能够以函数外部对象的形式访问响应
编辑 2:
我知道我做了什么。我应该简单地将 RootObject 重命名为 ResponseType,而不是在 ResponseType 中嵌套所有内容。
【问题讨论】:
-
你还没有定义任何字段,只是更多的类 - 你到底想在响应下访问什么?
-
它将您需要的所有类都放在另一个类中,这真是太可怕了。我建议将该外部类更改为命名空间。
-
你想完成什么?你为什么决定这个结构?你想在哪里访问
results变量? -
我编辑了问题以获取有关我的意图的更多信息。
-
如果您使用 Visual Studio,比 json2csharp 更好的选择是内置函数 Edit -> Paste Special -> Paste JSON As Classes。这应该会给你类似于我下面的答案的代码。
标签: c# visual-studio intellisense