c# 将带有 guid 的文本反序列化为 json [英] c# deserializing text with guids to json

查看:60
本文介绍了c# 将带有 guid 的文本反序列化为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务合同 (solr) 中得到以下 json

I get the following json from a service contract (solr)

{"highlighting":{
   "394c65f1-dfb1-4b76-9b6c-2f14c9682cc9":{
      "PackageName":["- <em>Testing<em> channel twenty."]},
   "baf8434a-99a4-4046-8a4d-2f7ec09eafc8":{
      "PackageName":["- <em>Testing<em> channel twenty."]},
   "0a699062-cd09-4b2e-a817-330193a352c1":{
     "PackageName":["- <em>Testing<em> channel twenty."]},
   "0b9ec891-5ef8-4085-9de2-38bfa9ea327e":{
     "PackageName":["- <em>Testing<em> channel twenty."]}}
}

当使用 http://json2csharp.com/ 时,我无法反序列化它,因为 guids 在属性中名称字段.
有没有办法可以将它反序列化为 c# 对象(我可以将 guid 命名为另一个名称,即 Id)?
谢谢.

when using http://json2csharp.com/ i can't deserialize it because the guids are in the attribute names field.
Is there a way I can deserialize this to c# object (I can call name the guid another name i.e. Id)?
thanks.

推荐答案

您应该将此 JSON 字符串反序列化为 Dictionary:

You should deserialize this JSON string as Dictionary<Guid, Package>:

示例:

string json = @"{
""326EAFDC-3553-4AA6-9D3B-79CC666A264C"": {...},
""70C57804-A9AF-41FC-9867-42C621E5A465"": {...}    
}";
var obj = JsonConvert.DeserializeObject<Dictionary<Guid, Package>>(json);

这篇关于c# 将带有 guid 的文本反序列化为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆