我在使用C#和asp.net
将Collection + JSON(http://amundsen.com/media-types/collection/format/)反序列化为对象时出现问题 JSON格式:
{
"collection": {
"version": "1.0",
"href": "http://www.example.com/api/",
"links":
[{
"href": "http://example.com/api/issues",
"rel": "issuesLink",
"name": "issuesLink",
"render": "link",
"prompt": "All issues ordered by number"
}],
"queries":
[{
"href": "https:\/\/example.com\/api\/search\/{field}\/{value}",
"rel": "searchByField",
"name": "FieldSearch",
"prompt": "Search by field",
"data":
[{
"name": "name",
"value": "field"
},
{
"name": "value",
"value": ""
}]
}]
}
}
我使用(或不使用)JSON.net没有任何问题,但无论如何都无法正确地反序列化。我有JSON
public class FPResponse
{
[JsonProperty("collection")]
//I have tried using List<string> too
// public Billboard collection executes the code but returns null for o
public string collection { get; set; }
}
public class Billboard
{
[JsonProperty("version")]
public string version { get; set; }
[JsonProperty("href")]
public string href { get; set; }
[JsonProperty("links")]
public IList<LinkSet> links { get; set; }
}
using (var reader = new StreamReader(dataStream))
{
string rtn = reader.ReadToEnd(); //has the JSON string
var o = JsonConvert.DeserializeObject<FPResponse>(rtn);
}
使用JSON.NET的错误:附加信息:读取字符串时出错。意外的令牌:StartObject。路径&#39;收集&#39;,第1行,第15位。
感谢您的帮助......
答案 0 :(得分:2)
collection
不是string
您的声明应如下:
public class Link
{
public string href { get; set; }
public string rel { get; set; }
public string name { get; set; }
public string render { get; set; }
public string prompt { get; set; }
}
public class Datum
{
public string name { get; set; }
public string value { get; set; }
}
public class Query
{
public string href { get; set; }
public string rel { get; set; }
public string name { get; set; }
public string prompt { get; set; }
public List<Datum> data { get; set; }
}
public class Collection
{
public string version { get; set; }
public string href { get; set; }
public List<Link> links { get; set; }
public List<Query> queries { get; set; }
}
public class FPResponse
{
public Collection collection { get; set; }
}
您可能想访问该网站http://json2csharp.com/