我正在使用Google Translation API来检测字符串的语言。 API返回此JSON:
{
"data": {
"detections": [
[
{
"confidence": 0.37890625,
"isReliable": false,
"language": "ro"
}
]
]
}
}
我还没有找到反序列化的方法。我正在使用System.Runtime.Serialization
,这是我的代码:
[DataContract]
public class GoogleTranslationResponse
{
[DataMember(Name = "data")]
public Data Data { get; set; }
}
[DataContract]
public class Data
{
[DataMember(Name = "detections")]
public List<Detection> Detections { get; set; }
}
[DataContract]
public class Detection
{
[DataMember(Name = "confidence")]
public decimal Confidence { get; set; }
[DataMember(Name = "isReliable")]
public bool IsReliable { get; set; }
[DataMember(Name = "language")]
public string Language { get; set; }
}
// ...
var jsonSerializer = new DataContractJsonSerializer(typeof(GoogleTranslationResponse));
result = (GoogleTranslationResponse)jsonSerializer.ReadObject( new MemoryStream(Encoding.Unicode.GetBytes(responseData)));
我得到了这个结果:
Confidence: 0
IsReliable:false
Language:null
答案 0 :(得分:3)
在你的JSON中,"detections"
的值是一个2d锯齿状数组:
"detections": [ [ { ... } ] ]
因此,您的模型需要通过使用嵌套集合来反映这一点:
[DataContract]
public class Data
{
[DataMember(Name = "detections")]
public List<List<Detection>> Detections { get; set; }
}