将 JSON 反序列化为引发异常的 c# 对象
Deserialize JSON to c# object raising an exception
你能帮我把下面的JSON反序列化成c#吗。
[
{
"detectedLanguage": {
"language": "en",
"score": 10.0
},
"translations": [
{
"text": "",
"to": "da"
},
{
"text": "",
"to": "da"
}
]
}
]
我使用了以下 c# 类 进行反序列化,但出现异常。
public class DetectedLanguage
{
public string language { get; set; }
public int score { get; set; }
}
public class Translation
{
public string text { get; set; }
public string to { get; set; }
}
public class RootObject
{
public DetectedLanguage detectedLanguage { get; set; }
public List<Translation> translations { get; set; }
}
我的反序列化代码是:
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
异常
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'RootObject' because the type requires a JSON object (e.g.
{"name":"value"}) to deserialize correctly. To fix this error either
change the JSON to a JSON object (e.g. {"name":"value"}) or change the
deserialized type to an array or a type that implements a collection
interface (e.g. ICollection, IList) like List that can be
deserialized from a JSON array. JsonArrayAttribute can also be added
to the type to force it to deserialize from a JSON array. Path '',
line 1, position 1.
编辑为完整答案:
using Newtonsoft.Json;
class Program
{
public partial class RootObject
{
[JsonProperty("detectedLanguage")]
public DetectedLanguage DetectedLanguage { get; set; }
[JsonProperty("translations")]
public Translation[] Translations { get; set; }
}
public partial class DetectedLanguage
{
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("score")]
public long Score { get; set; }
}
public partial class Translation
{
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("to")]
public string To { get; set; }
}
public partial class RootObject
{
public static RootObject[] FromJson(string jsonresponse) => JsonConvert.DeserializeObject<RootObject[]>(jsonresponse);
}
static void Main(string[] args)
{
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = RootObject.FromJson(jsonResponse);
System.Console.WriteLine(result[0].DetectedLanguage.Language); //outputs "en"
}
}
score 属性 有时持有浮点值,但在我的 c# class 中,有数据类型 int 导致异常。在@Ivan Salo的评论之前我没有注意到。将数据类型 int 更改为 float 修复了我的 issue.I 也使用 List 反序列化 @ 建议的 JSON Jon Skeet 在评论区。
public class DetectedLanguage
{
public string language { get; set; }
public float score { get; set; }
}
你能帮我把下面的JSON反序列化成c#吗。
[
{
"detectedLanguage": {
"language": "en",
"score": 10.0
},
"translations": [
{
"text": "",
"to": "da"
},
{
"text": "",
"to": "da"
}
]
}
]
我使用了以下 c# 类 进行反序列化,但出现异常。
public class DetectedLanguage
{
public string language { get; set; }
public int score { get; set; }
}
public class Translation
{
public string text { get; set; }
public string to { get; set; }
}
public class RootObject
{
public DetectedLanguage detectedLanguage { get; set; }
public List<Translation> translations { get; set; }
}
我的反序列化代码是:
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
异常
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
编辑为完整答案:
using Newtonsoft.Json;
class Program
{
public partial class RootObject
{
[JsonProperty("detectedLanguage")]
public DetectedLanguage DetectedLanguage { get; set; }
[JsonProperty("translations")]
public Translation[] Translations { get; set; }
}
public partial class DetectedLanguage
{
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("score")]
public long Score { get; set; }
}
public partial class Translation
{
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("to")]
public string To { get; set; }
}
public partial class RootObject
{
public static RootObject[] FromJson(string jsonresponse) => JsonConvert.DeserializeObject<RootObject[]>(jsonresponse);
}
static void Main(string[] args)
{
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
var result = RootObject.FromJson(jsonResponse);
System.Console.WriteLine(result[0].DetectedLanguage.Language); //outputs "en"
}
}
score 属性 有时持有浮点值,但在我的 c# class 中,有数据类型 int 导致异常。在@Ivan Salo的评论之前我没有注意到。将数据类型 int 更改为 float 修复了我的 issue.I 也使用 List 反序列化 @ 建议的 JSON Jon Skeet 在评论区。
public class DetectedLanguage
{
public string language { get; set; }
public float score { get; set; }
}