Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查c中是否存在带有json响应体的属性值#_C#_Json_Json.net - Fatal编程技术网

C# 检查c中是否存在带有json响应体的属性值#

C# 检查c中是否存在带有json响应体的属性值#,c#,json,json.net,C#,Json,Json.net,有没有办法检查Json.NET中是否存在属性值 例如:如果JSON响应主体如下所示: [ { 'name': 'S1', 'active': true, 'deviceId': 123, }, { 'name': 'S2', 'active': true, 'deviceId': 123, }, { 'name': 'S3', 'active': true, 'deviceId': 123, } ] // The type of object i

有没有办法检查Json.NET中是否存在属性值

例如:如果JSON响应主体如下所示:

[
 {
  'name': 'S1',
  'active': true,
  'deviceId': 123,
 },
 {
  'name': 'S2',
  'active': true,
  'deviceId': 123,
 },
 {
  'name': 'S3',
  'active': true,
  'deviceId': 123,
 }
]
// The type of object inside your array
public class Dto
{
  public string name;
  public bool active;
  public int deviceId;
}

//Then in the controller method receiving this call:
[HttpPost("containss1")]
public string ContainsS1(Dto[] payload)
{
  return payload.Any(dto => dto.name == "S1");
}

我们如何检查响应主体是否包含“S1”

如果要在C#中使用json,首先要反序列化为实类型。在这种情况下,看起来像这样:

[
 {
  'name': 'S1',
  'active': true,
  'deviceId': 123,
 },
 {
  'name': 'S2',
  'active': true,
  'deviceId': 123,
 },
 {
  'name': 'S3',
  'active': true,
  'deviceId': 123,
 }
]
// The type of object inside your array
public class Dto
{
  public string name;
  public bool active;
  public int deviceId;
}

//Then in the controller method receiving this call:
[HttpPost("containss1")]
public string ContainsS1(Dto[] payload)
{
  return payload.Any(dto => dto.name == "S1");
}
当然,这是一个非常基本的例子,所以根据您的用例,您将有更多的例子。然而,这里可以调用方法
ContainsS1
,并返回数组是否包含“S1”


另外,您通常使用PascalCase而不是camelCase,并告诉序列化程序在外壳之间进行转换。JSON.NET可以为您做到这一点。

您可以将JSON解析为实例,然后检查任何类型的子标记是否具有
S1

var json=JArray.Parse(jsonString); var exist=json.degenantsandself() 第()类 .Any(p=>p.Value.Value()==“S1”);
如果您需要检查所有响应正文值并且不想使用certan密钥

public static void Main(string[] args)
{
    string initialJSON = "[{'name': 'S1','active': true,'deviceId': 123,},{'name': 'S2','active': true,'deviceId': 123,},{'name': 'S3','active': true,'deviceId': 123,}]";
    List<JObject> deserializedJSON = JsonConvert.DeserializeObject<List<JObject>>(initialJSON);
    Console.WriteLine(deserializedJSON.Any(x => x.PropertyValues().Values().Any(y => y.ToString().Equals("S1"))));
    Console.ReadLine();
}
publicstaticvoidmain(字符串[]args)
{
字符串initialJSON=“[{name':'S1','active':true,'deviceId':123,},{'name':'S2','active':true,'deviceId':123,},{'name':'S3','active':true,'deviceId':123,}]”;
List deserializedJSON=JsonConvert.DeserializeObject(initialJSON);
Console.WriteLine(反序列化djson.Any(x=>x.PropertyValues().Values().Any(y=>y.ToString().Equals(“S1”)));
Console.ReadLine();
}

这不是有效的JSON。-
var isItThere=Welcome.FromJson(jsonhere).Any(z=>z.Name==“S1”)。
您是将JSON反序列化为某个数据模型,还是将其解析为
JToken
层次结构?@dbc正在将其解析为JToken层次结构。@akhildev是否回答了您的问题,是否需要更多详细信息?