简体   繁体   English

从 JSON 字符串中获取值,该字符串在变量名称中具有特殊标记 C#

[英]Get values from a JSON string which has special tag in variable names C#

I get following JSON where i need to extract few data from it.我关注 JSON,我需要从中提取一些数据。

{  
 "id":"400xxtc200",
 "state":"failed",
 "name":"barbaPapa",
 "content-exception":"AccessDenied",
 "input-parameters":[  
    {  
       "value":{  
          "string":{  
             "value":"In"
          }
       },
       "type":"string",
       "name":"Operation",
       "scope":"local"
    },
    {  
       "value":{  
        "string":{  
           "value":"bila"
        }
     },
     "type":"string",
     "name":"VMName",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"txtc"
        }
     },
     "type":"string",
     "name":"PSUser",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"dv1"
        }
     },
     "type":"string",
     "name":"Datacenter",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"tpc"
        }
     },
     "type":"string",
     "name":"ServiceArea",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"103"
        }
     },
     "type":"string",
     "name":"SQN",
     "scope":"local"
  }
 ],
 "output-parameters":[  
  {  
     "type":"Array/string",
     "name":"tag",
     "scope":"local"
  },
  {  
     "value":{  
        "string":{  
           "value":"AccessDenied"
        }
     },
     "type":"string",
     "name":"Error",
     "scope":"local"
  }
 ]
}

I am trying to deserialize the JSON object in to a dynamic object which was successful.I'm using following C# code for the work我正在尝试将 JSON object 反序列化为成功的动态 object。我正在使用以下 C# 代码进行工作

string responseText = reader.ReadToEnd();
dynamic in_values =    JsonConvert.DeserializeObject(responseText);
string state = in_values.state;

If you can see I have some tag names with hyphen in the JSON string.如果您看到我在 JSON 字符串中有一些带连字符的标签名称。

For example output-parameters例如output-parameters

I cant use the dot operate because then it will be as like follow我不能使用点操作,因为那样就会像下面一样

in_values.output-parameters;

How to extract those values from the JSON string.如何从 JSON 字符串中提取这些值。

You can use JsonProperty in this case.在这种情况下,您可以使用JsonProperty

public class SampleClass
{
    public string id { get; set; }
    public string state { get; set; }
    public string name { get; set; }

    [JsonProperty(PropertyName = "content-exception")]
    public string content_exception { get; set; }
    [JsonProperty(PropertyName = "input-parameters")]
    public List<InputParameter> input_parameters { get; set; }
    [JsonProperty(PropertyName = "output-parameters")]
    public List<OutputParameter> output_parameters { get; set; }
}

string json = File.ReadAllText("abc.txt");
SampleClass obj = JsonConvert.DeserializeObject<SampleClass>(json);
List<OutputParameter>  ls = obj.output_parameters;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM