需要提取属性和值(s)从json字符串

本文关键字:json 字符串 提取 属性 | 更新日期: 2023-09-27 18:09:26

我有一个Json字符串,我将其反序列化并转换为具有2个键的字典。我对键(服务)感兴趣,它的值包含一串服务,每个服务都有自己的属性,都在一行中,由逗号和括号分隔。我希望能够遍历这些服务并获取每个服务的属性。我以为正则表达式可以做到,但是我找不到匹配的模式'

 responseDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(response);
 var services = responseDictionary["services"]

返回的值是这样的模式

"['r'n  {'r'n    '"name'": '"extract'",'r'n    '"type'": '"FeatureServer'"'r'n  },'r'n  {'r'n    '"name'": '"extract'",'r'n    '"type'": '"MapServer'"'r'n  }'r'n]"

有两个服务,

extract——of type of featureserver.

extract——of type mapserver

我怎么做才能得到这两个服务和它们的类型?

需要提取属性和值(s)从json字符串

格式化后的JSON如下:

[{
    "name": "extract",
    "type": "FeatureServer"
},
{
    "name": "extract",
    "type": "MapServer"
}]

可以映射到类:

public class Service
{
    public string name { get; set; }
    public string type { get; set; }
}

可以这样反序列化

List<Service> services = JsonConvert.DeserializeObject<List<Service>>(response);

每个服务的循环:

foreach(Service s in services)
{
    string name = s.name;
    string type = s.type;
}