繁体   English   中英

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

[英]Error:Cannot access child value on Newtonsoft.Json.Linq.JValue

How i can access below object type value which is coming as request body from data factory output of another function app in http trigger function. 现在我需要对 http 触发器 function 中的这些输出执行一些操作。 { "functionName": "GoogleAuth", "method": "POST", "headers": {}, "body": { "Response": "[{"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 (西欧)", "executionDuration": 0, "durationInQueue": { "integrationRuntimeQueue": 0 }, "billingReference": {" activityType": "ExternalActivity", "billableDuration": [ { "meterType": "AzureIR", "duration": 0.016666666666666666, "unit": "Hours" } ] } } }

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

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 上的子值。

"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

调试截图如下:

在此处输入图像描述

暂无
暂无

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

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