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

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

我如何访问以下 object 类型的值,该值作为请求 body 来自 http 触发器函数中另一个函数应用程序的数据工厂输出。 现在我需要在 http 触发器函数中对这些输出执行一些操作。 { “功能名称”:“GoogleAuth”, “方法”:“POST”, “headers”:{}, “body”:{ "响应": "[{"id":"hjk","name":"abc","description":"hki","brand":"鸟眼","ean":"125","mediaStorageKey ":"124","maxQuantity":6,"价格":1.75,"尺寸":224.0,"sizeUnits":"克"}]", "effectiveIntegrationRuntime": "DefaultIntegrationRuntime(西欧)", “执行持续时间”:0, “durationInQueue”:{ “集成运行时队列”: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 上的 child 值。

"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

调试截图如下: