【问题标题】:count Jarray in C#在 C# 中计算 Jarray
【发布时间】:2021-08-20 23:18:27
【问题描述】:

我需要在 httptrigger 函数中计算请求体中的数据。数据以对象类型的形式出现。 我正在反序列化 reqd 主体,如下所示。以下是我在 req.body 中获取的对象类型数据。

{
  "Response": [
    {
      "id": "1",
      "name": "Warburtons Medium Sliced Soft White Bread 400g",
      "description": "Warburtons Medium Sliced White 400G",
      "brand": "Warburtons",
      "ean": "123",
      "mediaStorageKey": "b",
      "maxQuantity": 6,
      "price": 0.95,
      "size": 400,
      "sizeUnits": "Grams"
    },
    {
      "id": "a",
      "name": "Co-op Orvieto Classico 75cl",
      "description": "Co-op Orvieto Classico 75CL",
      "brand": "Co-op",
      "ean": "489",
      "mediaStorageKey": "c",
      "maxQuantity": 6,
      "price": 5.5,
      "size": 75,
      "sizeUnits": "Centilitres"
    },
    {
      "id": "kl",
      "name": "Co Op Garden Peas in Water 290g",
      "description": "Co-op Garden Peas 290G",
      "brand": "Co Op",
      "ean": "678",
      "mediaStorageKey": "f",
      "maxQuantity": 6,
      "price": 0.45,
      "size": 175,
      "sizeUnits": "Grams"
    }
  ]
}
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic body = JsonConvert.DeserializeObject(requestBody);
             
dynamic data = body["Response"];

直到这个我得到如下数据。

{[{"id":"1","name":"b"},{"id":"f","name":"j"}]}

但现在我无法计算在这种情况下给出 2 的这些数据,因为我必须申请循环。 CountCount() 都没有在这里工作。我收到以下错误。

Newtonsoft.Json.Linq.JValue 不包含Count 的定义

【问题讨论】:

  • 您能否发布完整的requestBody JSON(至少足以让我们运行您的dynamic data = body["Response"])并获得与您显示的内容相似的内容(顺便说一下,这不是有效的 JSON)
  • 能否请edit 分享minimal reproducible example 的问题?如果我在您的问题中使用 JSON 并尝试解析它,则会收到错误 Invalid property identifier character: [. Path '', line 1, position 1.,请参阅 dotnetfiddle.net/FVmqgM。问题是 JSON 格式不正确——它有额外的外括号 {},不应该出现。
  • 如果我通过删除外括号来修复 JSON,您的代码会生成 *Accessed JArray values with invalid key value: "Response". Int32 array index expected. 异常,因为根 JSON 容器是数组而不是对象,因此没有 "Response" 属性。见dotnetfiddle.net/KYJuhG
  • 我的疯狂猜测是原始 JSON 看起来像 {"Response" : [{"id":"1","name":"b"},{"id":"f","name":"j"}]},当他获得响应值时,它变成了 [{"id":"1","name":"b"},{"id":"f","name":"j"}],并且调试器用 {} 括号括起来显示它。只是猜测
  • 您的疯狂猜测似乎是合理的,但minimal reproducible example 会澄清。特别是因为问题是使用dynamic 来处理所有事情,这使得通过检查很难知道发生了什么(并且可能导致问题)。

标签: c# asp.net-core json.net azure-http-trigger


【解决方案1】:

idname这两个字段创建一个类

public class Item {
  public Int Id {get; set;}
  public String Name {get; set;}
}

并指定反序列化值的类型:

dynamic body = JsonConvert.DeserializeObject(requestBody);
var rows = JsonConvert.DeserializeObject<List<Item>>(body["Response"]);

现在你可以使用rows.Count();

【讨论】:

  • 抛出错误。 'Newtonsoft.Json.JsonConvert.DeserializeObject>(string)' 的最佳重载方法匹配有一些无效参数。
【解决方案2】:

如果您只需要知道 Response 集合中的条目数量,那么您只需使用 JObjectJArray 即可:

string rawJson = ...;
JObject semiParsedJson = JObject.Parse(rawJson);
JArray entries = (JArray)semiParsedJson["Response"];
int count = entries.Count;

【讨论】:

  • 我按照你的建议尝试了,但它显示错误。 string requestBody = await new StreamReader(req.Body).ReadToEndAsync();动态主体 = JsonConvert.DeserializeObject(requestBody); JObject semiParsedJson = JObject.Parsebody JArray 条目 = (JArray)semiParsedJson["Response"]; int count = entries.Count;无法将类型为“Newtonsoft.Json.Linq.JValue”的对象转换为类型“Newtonsoft.Json.Linq.JArray”。
  • @shashankshekhar 这意味着复制粘贴的响应没有反映现实。能更新一下吗?
  • 数据作为对象类型来自另一个函数应用的数据工厂输出的请求正文
  • @shashankshekhar 在提供的示例中,Response 是一个集合 ([]),而不是一个对象 ({})。当我编辑你的问题时,也许我已经自动更正了一些东西。你能用requestBody变量的内容覆盖它吗?
猜你喜欢
  • 2020-12-08
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 2021-06-01
  • 2011-02-23
相关资源
最近更新 更多