C# 使用RestSharp客户端反序列化嵌套JSON
我想使用RESTAPI并反序列化嵌套的JSON响应。我使用json2csharp.com创建了这些类 JSON消费C# 使用RestSharp客户端反序列化嵌套JSON,c#,json,deserialization,C#,Json,Deserialization,我想使用RESTAPI并反序列化嵌套的JSON响应。我使用json2csharp.com创建了这些类 JSON消费 { id: 32, name: "test supply object", formId: 4, sortOrder: 0, created: 1572902163907, creator: "jsingler", attributes: [ { id: 144, attribute: { attribut
{
id: 32,
name: "test supply object",
formId: 4,
sortOrder: 0,
created: 1572902163907,
creator: "jsingler",
attributes: [
{
id: 144,
attribute: {
attributeName: "Description",
attributeType: "TextArea",
id: 8
},
attributeValue: "for development testing. do not delete or use."
},
{
id: 145,
attribute: {
attributeName: "Quantity",
attributeType: "NumberLong",
id: 10
},
attributeValue: "4"
}
]
}
JSON2CSHARP.COM输出
public partial class Asset
{
[JsonProperty(PropertyName = "id")]
public int id { get; set; }
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "formId")]
public int formId { get; set; }
[JsonProperty(PropertyName = "sortOrder")]
public int sortOrder { get; set; }
[JsonProperty(PropertyName = "created")]
public long created { get; set; }
[JsonProperty(PropertyName = "creator")]
public string creator { get; set; }
public List<Attribute> attributes { get; set; }
}
public partial class Attribute
{
[JsonProperty(PropertyName = "ida")]
public int id { get; set; }
[JsonProperty(PropertyName = "attribute")]
public List<Attribute1> attribute { get; set; }
[JsonProperty(PropertyName = "attributeValue")]
public string attributeValue { get; set; }
}
public partial class Attribute1
{
[JsonProperty(PropertyName = "attributeName")]
public string attributeName { get; set; }
[JsonProperty(PropertyName = "attributeType")]
public string attributeType { get; set; }
[JsonProperty(PropertyName = "ida1")]
public int id { get; set; }
}
这总是失败。您的JSON无效。转到并检查所有错误
属性名称必须在引号之间,如果希望它是一个数组,则需要将其设置为一个数组,并在json的开头和结尾添加方括号-[]。首先确保json有效。你可以看看这个 json属性必须在引号内,如下所示 “id” 除此之外,根对象不是列表。因此,您必须将反序列化代码更改为:
var AssetList = JsonConvert.DeserializeObject<Asset>(response.Content);
var AssetList=JsonConvert.DeserializeObject(response.Content);
您可以发布正在解析的实际JSON吗。样品看起来不错,谢谢。这很有帮助。我现在在格式化RestSharp Put语句时遇到问题。@AaronComstock很高兴听到这个消息。如果你能投票并签署正确的答案,如果它解决了你的问题,我会很高兴。JSON格式正确。我只是将Chrome渲染的示例JSON粘贴到更可读的格式中。
[HttpPut]
public void Send(Asset newJA, int Qty)
{
var client = new RestClient("URL_TO_USE" + newJA.id + ".json");
var request = new RestRequest("", Method.PUT);
request.AddObject(newJA);
client.Authenticator = new HttpBasicAuthenticator("USERNAME", "PW");
client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);
Debug.WriteLine(response.Content);
});
}
var AssetList = JsonConvert.DeserializeObject<Asset>(response.Content);