首页 / JSON / 可能的JSON到C#中的字典
可能的JSON到C#中的字典
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了可能的JSON到C#中的字典,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2327字,纯文字阅读大概需要4分钟。
内容图文
![可能的JSON到C#中的字典](/upload/InfoBanner/zyjiaocheng/681/14e756c49e8c4e19855601db0062fdfe.jpg)
我对JSON的概念有些新鲜,所以在这里忍受.
我有一个方法可以返回以下冗长而复杂的字符串:
[{\"identifierType\":\".Identifier\",\"title\":\"Test\",\"typeName\":\"Application1\",\"key\":\"1\",\"internalId\":1},{\"identifierType\":\".Identifier\",\"title\":\"Test2\",\"typeName\":\"Application2\",\"key\":\"2\",\"internalId\":15},{\"identifierType\":\".Identifier\",\"title\":\"Test3\",\"typeName\":\"Application3\",\"key\":\"3\",\"internalId\":1124},{\"identifierType\":\".Identifier\",\"title\":\"Test4\",\"typeName\":\"Application4\",\"key\":\"4\",\"internalId\":2241}]
她的眼睛长而丑陋,但这是我必须配合的.
经过一番谷歌搜索后,我发现这是一个JSON字符串,并且可以将其映射为C#中的Dictionary.
现在,经过更多的谷歌搜索,我得出了以下结论:
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(s);
在这种情况下,s是我的长而丑陋的字符串.
问题是运行时会引发错误.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
‘System.Collections.Generic.Dictionary`2[System.String,System.String]’
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.
现在我变得像JSON中的白菜一样绿,对此我感到困惑.
我不是很美吗,不是我认为她是的JSON字符串吗?
如果她是,那么这在理论上是行不通的吗?
我还预见到一个问题,即s中似乎有多个对象.当我尝试将其映射到字典时不会造成麻烦吗?
我是否必须将映射到多个词典?
请指教.
解决方法:
您的json是一个数组,比如说YourJsonObject,因此您可以将其反序列化为List< YourJsonObject>.
http://json2csharp.com站点在这些scenairos中派上了用场,因此通过在其中粘贴json,它会发出以下类:
public class YourJsonObject
{
public string identifierType { get; set; }
public string title { get; set; }
public string typeName { get; set; }
public string key { get; set; }
public int internalId { get; set; }
}
和
var list = JsonConvert.Deserialize< List< YourJsonOject>> ;;
为您提供所需的东西.如果确实需要,可以建议将其反序列化为“词典列表”,作为我上方的注释者的建议,但由于它也包含整数,因此这种方法可能更好.
内容总结
以上是互联网集市为您收集整理的可能的JSON到C#中的字典全部内容,希望文章能够帮你解决可能的JSON到C#中的字典所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。