使用 Newtonsoft.Json 在 C# 中反序列化 Google 音译
作为初学者,我尝试反序列化 Google 音译器返回数组。这是一个像这样的 JSON 数组:
[{"ew" : "namaste","hws" : ["नमस्ते","नमसते","नमास्ते",]},]
太棒了!
这是我用于反序列化的 C# 类:
[Serializable]
public class googleTransliterator
{
[JsonProperty("ew")]
public String sourceWord { get; set; }
[JsonProperty("hws")]
public String[] transliteratedWords { get; set; }
}
最后:
using Newtonsoft.Json;
...
...
// return value from google transliteration API is in streamReader
var s = new JsonSerializer();
var gt = (googleTransliterator) s.Deserialize( new StringReader( streamReader.ReadToEnd()), typeof( googleTransliterator));
我得到的是:
Cannot create and populate list type GoogleTransliterator.googleTransliterator.
知道出了什么问题吗?
As a beginner I tried to deserialize Google transliterator return array. It's a JSON array like this:
[{"ew" : "namaste","hws" : ["नमस्ते","नमसते","नमास्ते",]},]
pretty awesome!
This is my c# class used for deserialization:
[Serializable]
public class googleTransliterator
{
[JsonProperty("ew")]
public String sourceWord { get; set; }
[JsonProperty("hws")]
public String[] transliteratedWords { get; set; }
}
and finally:
using Newtonsoft.Json;
...
...
// return value from google transliteration API is in streamReader
var s = new JsonSerializer();
var gt = (googleTransliterator) s.Deserialize( new StringReader( streamReader.ReadToEnd()), typeof( googleTransliterator));
And what I got is:
Cannot create and populate list type GoogleTransliterator.googleTransliterator.
Any idea what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果是 JSON 对象数组 (
[{...},]
),而不是普通对象 ({...}
)。如果你像这样改变你的代码应该可以工作:
The result is an array of JSON objects (
[{...},]
) instead of a plain object ({...}
).Your code should work if you change it like this: