需要从json字符串中提取属性和值

时间:2022-12-05 16:01:44

I have a Json string that I deserialized and turned into a dictionary that has 2 keys. I am interested in the key (services) which it's value contains h a string of services each with its own properties all in one line seperated by commas and parenthesis. I want to be able to loop over those services and get each one with it's properties. I thought regular expression would do it, but I cant find a matching pattern `

我有一个Json字符串,我反序列化并变成一个有2个键的字典。我感兴趣的是它的值包含的密钥(服务),每个服务都有自己的属性,所有这些属性都在一行中用逗号和括号分隔。我希望能够遍历这些服务并使用它的属性获取每个服务。我认为正则表达式会这样做,但我找不到匹配的模式`

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

The value I get back has this pattern

我得到的价值有这种模式

"[\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]"

there are 2 services,

有2项服务,

extract---of type featureserver.

提取---类型featureserver。

extract---of type mapserver

提取---类型mapserver

What can I do to get those 2 services with thier type ?

我该怎么做才能获得这两种类型的服务?

1 个解决方案

#1


2  

Your JSON after formating looks:

格式化之后你的JSON看起来:

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

And can be mapped to class:

并且可以映射到类:

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

So yo can deserialize it like this:

所以你可以像这样反序列化它:

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

And loop for each service:

并为每个服务循环:

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

#1


2  

Your JSON after formating looks:

格式化之后你的JSON看起来:

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

And can be mapped to class:

并且可以映射到类:

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

So yo can deserialize it like this:

所以你可以像这样反序列化它:

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

And loop for each service:

并为每个服务循环:

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