【发布时间】:2020-10-21 12:28:53
【问题描述】:
我很难在列表中绑定一个名为 url 的特定字段。
这是我的 json 格式:
{
id: 'someID',
name: 'someName.pdf',
url: {
download: 'https://mydomain/api_binary/v1/document/mcipv7sgq11c9e6e42upqapc7k '
}
}
这是我的模特
public class CantoItem
{
public string id { get; set; }
public string name { get; set; }
[JsonProperty("default")]
public DefaultNest Default { get; set; }
[JsonProperty("url")]
public ResultUrl Url { get; set; }
[JsonProperty("tag")]
public object Tag { get; set; }
}
public partial class ResultUrl
{
[JsonProperty("preview")]
public Uri Preview { get; set; }
[JsonProperty("download")]
public Uri Download { get; set; }
}
[JsonProperty("tag")]
public List<string> Tag { get; set; }
}
然后添加我拥有的值:
var cantoAssetMetadata = new List<CantoItem>();
foreach (JValue item in collection)
{
try
{
var collectionjObj = JObject.Parse(item.ToString());
cantoAssetMetadata.Add(new CantoItem()
{
id = collectionjObj["id"].ToString(),
name = collectionjObj["name"].ToString(),
Url = collectionjObj["url"].ToString(),
Tag = collectionjObj["tag"].ToString(),
});
}
catch (Exception ex)
{
Console.WriteLine($"Exeption: {ex}, {item}");
}
}
其他字段设置正确,只是 URL 不正确,我不知道如何正确设置值。任何帮助将不胜感激
【问题讨论】:
-
你在一个类对象上调用
.ToString()-Url = collectionjObj["url"].ToString(),Url是一个partial class称为ResultUrl