简体   繁体   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. 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. Now i need to perform some operation with these ouput 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 (West Europe)", "executionDuration": 0, "durationInQueue": { "integrationRuntimeQueue": 0 }, "billingReference": { "activityType": "ExternalActivity", "billableDuration": [ { "meterType": "AzureIR", "duration": 0.016666666666666666, "unit": "Hours" } ] } } } { "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" } ] } } }

I am trying to access it like this but is showing error.我正在尝试像这样访问它,但显示错误。

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"]);

Error:Cannot access child value on Newtonsoft.Json.Linq.JValue.错误:无法访问 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}]

First, please set a break point to check the requestBody value, because the above value is not a valid json.首先,请设置断点检查requestBody值,因为上面的值不是有效的 json。 You could search "Json Parse Online" and use online tools to verify it.您可以搜索“Json Parse Online”并使用在线工具进行验证。

After changing the response as below (remove the '"' before the '[' and after the ']'), your code works well如下更改响应后(删除 '[' 之前和 ']' 之后的 '"'),您的代码运行良好

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

Second, from your comment, the dynamic data value is:其次,根据您的评论,动态数据值为:

[{"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}] 

In this scenario, you can convert the data value to string, then, use JsonConvert.DeserializeObject() convert the string value, after that, you could get the id value.在这种情况下,您可以将data值转换为字符串,然后使用JsonConvert.DeserializeObject()转换字符串值,然后就可以得到 id 值。 code like this:像这样的代码:

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

The debug screenshot as below:调试截图如下:

在此处输入图像描述

暂无
暂无

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

相关问题 json无法访问Newtonsoft.Json.Linq.JValue上的子值 - json Cannot access child value on Newtonsoft.Json.Linq.JValue 无法访问 Newtonsoft.Json.Linq.JValue 上的子值 - Cannot access child value on Newtonsoft.Json.Linq.JValue “无法访问 Newtonsoft.Json.Linq.JValue 上的子值” - "Cannot access child value on Newtonsoft.Json.Linq.JValue" 动态Json-无法访问Newtonsoft.Json.Linq.JValue上的子值 - Dynamic Json - Cannot access child value on Newtonsoft.Json.Linq.JValue C#无法在Newtonsoft.Json.Linq.JValue上访问子值 - C# Cannot access child value on Newtonsoft.Json.Linq.JValue 访问JToken的值时获取异常-无法访问Newtonsoft.Json.Linq.JValue上的子值 - Getting Exception while accessing value of JToken - Cannot access child value on Newtonsoft.Json.Linq.JValue Neo4jConnection期间无法访问Newtonsoft.Json.Linq.JValue上的子值 - Cannot access child value on Newtonsoft.Json.Linq.JValue during Neo4jConnection System.InvalidOperationException:无法访问 Newtonsoft.Json.Linq.JValue 上的子值 - System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JValue C#如何处理NULL json值? Getting和InvalidOperationException:无法访问> Newtonsoft.Json.Linq.JValue上的子值 - C# How to handle NULL json values? Getting and InvalidOperationException: Cannot access child value on > Newtonsoft.Json.Linq.JValue “无法访问 Newtonsoft.Json.Linq.JValue 上的子值。” - "Cannot access child value on Newtonsoft.Json.Linq.JValue."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM