簡體   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