C# 在C中从api访问JSON对象#
我在做一个微软认知服务项目,包括语言检测和文本翻译 我从MicrosoftAPI接收Json,我想作为一个对象访问它。 我尝试了一些不同的方法,包括JsonConvert.DeserializeObject,并将其映射到我自己的对象。(下面的例子) //我找到了一个在线工具来生成适当的映射类C# 在C中从api访问JSON对象#,c#,json,asp.net-core,deserialization,microsoft-cognitive,C#,Json,Asp.net Core,Deserialization,Microsoft Cognitive,我在做一个微软认知服务项目,包括语言检测和文本翻译 我从MicrosoftAPI接收Json,我想作为一个对象访问它。 我尝试了一些不同的方法,包括JsonConvert.DeserializeObject,并将其映射到我自己的对象。(下面的例子) //我找到了一个在线工具来生成适当的映射类 public class DetectedLanguage { [JsonPropertyName("language")] public st
public class DetectedLanguage
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("score")]
public double Score { get; set; }
}
public class Translation
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("to")]
public string To { get; set; }
}
public class MyArray
{
[JsonPropertyName("detectedLanguage")]
public DetectedLanguage DetectedLanguage { get; set; }
[JsonPropertyName("translations")]
public List<Translation> Translations { get; set; }
}
public class Root
{
[JsonPropertyName("MyArray")]
public List<MyArray> MyArray { get; set; }
}
公共类检测语言
{
[JsonPropertyName(“语言”)]
公共字符串语言{get;set;}
[JsonPropertyName(“分数”)]
公共双倍分数{get;set;}
}
公共课翻译
{
[JsonPropertyName(“文本”)]
公共字符串文本{get;set;}
[JsonPropertyName(“收件人”)]
{get;set;}的公共字符串
}
公共类MyArray
{
[JsonPropertyName(“detectedLanguage”)]
公共检测语言检测语言{get;set;}
[JsonPropertyName(“翻译”)]
公共列表翻译{get;set;}
}
公共类根
{
[JsonPropertyName(“MyArray”)]
公共列表MyArray{get;set;}
}
//我得到的错误
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'textananlytics_test.Root' 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<T> 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.'
Newtonsoft.Json.JsonSerializationException:'无法将当前Json数组(例如[1,2,3])反序列化为类型'textananlytics_test.Root',因为该类型需要一个Json对象(例如{“name”:“value”})才能正确反序列化。
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。
路径“”,第1行,位置1。“
您可以更改
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
Root myDeserializedClass=JsonConvert.DeserializeObject(结果);
到
List List=JsonConvert.DeserializeObject(结果);
根myDeserializedClass=新根{MyArray=list};
在您的情况下,JSON必须具有下一个结构:
{
"MyArray": [
{ /* MyArray jobject */ },
{ /* MyArray jobject */ },
...
]
}
所以写了正确的处理方法
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result);
List<MyArray> list = JsonConvert.DeserializeObject<List<MyArray>>(result);
Root myDeserializedClass=new Root{MyArray=list};
{
"MyArray": [
{ /* MyArray jobject */ },
{ /* MyArray jobject */ },
...
]
}