【问题标题】:Error:Cannot access child value on Newtonsoft.Json.Linq.JValue错误:无法访问 Newtonsoft.Json.Linq.JValue 上的子值
【发布时间】:2021-08-21 11:10:25
【问题描述】:

我如何访问下面的对象类型值,该值作为请求正文来自 http 触发函数中另一个函数应用程序的数据工厂输出。 现在我需要在 http 触发函数中对这些输出执行一些操作。 { "functionName": "GoogleAuth", “方法”:“发布”, “标题”:{}, “身体”: { "响应": "[{"id":"hjk","name":"abc","description":"hki","brand":"Birds Eye","ean":"125","mediaStorageKey ":"124","maxQuantity":6,"price":1.75,"size":224.0,"sizeUnits":"Grams"}]", "effectiveIntegrationRuntime": "DefaultIntegrationRuntime (西欧)", “执行持续时间”:0, “持续时间队列”:{ “集成运行时间队列”:0 }, “计费参考”:{ "activityType": "外部活动", “计费持续时间”:[ { “meterType”:“AzureIR”, “持续时间”:0.016666666666666666, “单位”:“小时” } ] } } }

我正在尝试这样访问它,但显示错误。

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic body = JsonConvert.DeserializeObject(requestBody);
dynamic data = body["Response"];
product.OfferId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " :Convert.ToString(data[0]["id"]);

错误:无法访问 Newtonsoft.Json.Linq.JValue 上的子值。

【问题讨论】:

  • 此请求正文不是有效的 JSON
  • 我无法为我得到的请求主体做任何事情,因为这些值是来自数据工厂的另一个函数应用输出的对象类型。
  • 你能显示动态数据的 pls 值 = body["Response"];
  • [{"id":"b","name":"Seriously Tasty Pasties Traditional","description":"Seriously Tasty 传统牛肉蔬菜馅饼 199G","brand":"Seriously Tasty","ean":"6","mediaStorageKey":"7fb","maxQuantity":6,"price":0.55,"size":null,"sizeUnits":null}]

标签: c# .net-core json.net azure-data-factory-pipeline azure-http-trigger


【解决方案1】:
"Response": "[{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]

首先请设置断点检查requestBody的值,因为上面的值不是有效的json。您可以搜索“Json Parse Online”并使用在线工具进行验证。

如下更改响应后(删除 '[' 之前和 ']' 之后的 '"'),您的代码运行良好

string requestBody = "{\"Response\": [{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]}";
dynamic body = Newtonsoft.Json.JsonConvert.DeserializeObject(requestBody);
dynamic  data = body["Response"];
                     
var  offerId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " : Convert.ToString(data[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

其次,从你的评论来看,动态数据值为:

[{"id":"b","name":"Seriously Tasty Pasties Traditional","description":"Seriously Tasty Traditional Beef and Vegetable Pasty 199G","brand":"Seriously Tasty","ean":"6","mediaStorageKey":"7fb","maxQuantity":6,"price":0.55,"size":null,"sizeUnits":null}] 

在这种情况下,您可以将data 值转换为字符串,然后使用JsonConvert.DeserializeObject() 转换字符串值,然后就可以得到id 值。代码如下:

string requestBody = "{\"Response\": [{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]}";
dynamic body = Newtonsoft.Json.JsonConvert.DeserializeObject(requestBody);
dynamic  data = body["Response"];
                     
var  offerId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " : Convert.ToString(data[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

var datastr = Convert.ToString(data);

var datavalue = "[{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]";
dynamic databody = Newtonsoft.Json.JsonConvert.DeserializeObject(datastr); //for test purpose, change datastr to datavalue.

var Id = string.IsNullOrEmpty(Convert.ToString(databody[0]["id"])) ? " " : Convert.ToString(databody[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

调试截图如下:

【讨论】:

  • 谢谢..我已经粘贴(编辑)完整的请求正文,我从另一个函数应用程序的数据工厂输出中获得。如果您将所有内容都放入 json 验证中,它是一个有效的 json。任何建议.
  • 嗨@shashankshekhar,请检查this screenshot,编辑后的请求正文似乎仍然有一些错误,将响应的值从字符串更改为数组后,问题就消失了。可能是你不能改变响应体,如果是这种情况,请参考我的回复中的第二种方法,在你得到响应体后,你可以将它转换为字符串(结果应该像数据值的值) ,然后,使用 JsonConvert.DeserializeObject() 转换字符串值,之后就可以得到 id 值了。
猜你喜欢
  • 2021-01-23
  • 2021-08-09
  • 1970-01-01
  • 2019-08-24
  • 2018-12-19
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 2021-08-23
相关资源
最近更新 更多