为什么我的 JavaScriptSerializer().Deserialize() 不工作?
Why is my JavaScriptSerializer().Deserialize() is not working?
我从 Box API 电话中得到这个 JSON:
{"total_count":4,
"entries":[
{"type":"folder","id":"3102883693","sequence_id":"0","etag":"0","name":"Deployments"},
{"type":"folder","id":"3460455852","sequence_id":"0","etag":"0","name":"MARKETING"},
{"type":"folder","id":"2535410485","sequence_id":"1","etag":"1","name":"Plans"},
{"type":"folder","id":"3132381455","sequence_id":"0","etag":"0","name":"Projects"},
],
"offset":0,
"limit":100,
"order":[
{"by":"type","direction":"ASC"},
{"by":"name","direction":"ASC"}
]
}
我尝试将其放入 class 但我无法获取列表:
var folders = new JavaScriptSerializer().Deserialize<List<FolderItems>>(response.Content);
这是我的 classes:
public class FolderItems
{
public int total_count { get; set; }
public List<Entry> entries { get; set; }
public int offset { get; set; }
public int limit { get; set; }
public List<Order> order { get; set; }
}
public class Entry
{
public string type { get; set; }
public int id { get; set; }
public int sequence_id { get; set; }
public string etag { get; set; }
public string name { get; set; }
}
public class Order
{
public string by { get; set; }
public string direction { get; set; }
}
根据您的 JSON,您只有一个外部对象,而不是列表。
var folder = new JavaScriptSerializer().Deserialize<FolderItems>(response.Content);
您应该反序列化为单个 FolderItems
对象,其中包含该对象的条目列表。
我从 Box API 电话中得到这个 JSON:
{"total_count":4,
"entries":[
{"type":"folder","id":"3102883693","sequence_id":"0","etag":"0","name":"Deployments"},
{"type":"folder","id":"3460455852","sequence_id":"0","etag":"0","name":"MARKETING"},
{"type":"folder","id":"2535410485","sequence_id":"1","etag":"1","name":"Plans"},
{"type":"folder","id":"3132381455","sequence_id":"0","etag":"0","name":"Projects"},
],
"offset":0,
"limit":100,
"order":[
{"by":"type","direction":"ASC"},
{"by":"name","direction":"ASC"}
]
}
我尝试将其放入 class 但我无法获取列表:
var folders = new JavaScriptSerializer().Deserialize<List<FolderItems>>(response.Content);
这是我的 classes:
public class FolderItems
{
public int total_count { get; set; }
public List<Entry> entries { get; set; }
public int offset { get; set; }
public int limit { get; set; }
public List<Order> order { get; set; }
}
public class Entry
{
public string type { get; set; }
public int id { get; set; }
public int sequence_id { get; set; }
public string etag { get; set; }
public string name { get; set; }
}
public class Order
{
public string by { get; set; }
public string direction { get; set; }
}
根据您的 JSON,您只有一个外部对象,而不是列表。
var folder = new JavaScriptSerializer().Deserialize<FolderItems>(response.Content);
您应该反序列化为单个 FolderItems
对象,其中包含该对象的条目列表。