“无法访问 Newtonsoft.Json.Linq.JValue 上的子值”

Posted

技术标签:

【中文标题】“无法访问 Newtonsoft.Json.Linq.JValue 上的子值”【英文标题】:"Cannot access child value on Newtonsoft.Json.Linq.JValue" 【发布时间】:2021-01-23 06:16:24 【问题描述】:

我的 JSON 文件

"data":
[
"id":"1","user_code":"C016482","name":"CART 1","details":"[\"Id\":15476,\"Name2\":\"AQUAFRESHIG TEETH (6+)\",\"SalesPriceVM\":250,\"DiscountPercentVM\":0,\"product_quantity\":3,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=15476\",\"Id\":22514,\"Name2\":\"BABE ANTI OILY DANDRUFF SHAMPOO 250 ML\",\"SalesPriceVM\":1800,\"DiscountPercentVM\":0,\"product_quantity\":1,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=22514\",\"Id\":19886,\"Name2\":\"ALOE VERA GEL\",\"SalesPriceVM\":1674,\"DiscountPercentVM\":0,\"product_quantity\":1,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=19886\"]","created_at":"2020-05-20 11:52:49","updated_at":"2020-05-20 11:52:49",
"id":"2","user_code":"C020552","name":"CART1","details":"[\"Id\":15480,\"Name2\":\"LISTERINE MOUTHWASH ORIGINAL 500 ML\",\"SalesPriceVM\":460,\"DiscountPercentVM\":0,\"product_quantity\":1,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=15480\",\"Id\":20572,\"Name2\":\"SAVLON ACTIVE HAND WASH RF 1000ML\",\"SalesPriceVM\":230,\"DiscountPercentVM\":0,\"product_quantity\":1,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=20572\",\"Id\":25899,\"Name2\":\"HANDISANITTIZER 200ML SOLUTION\",\"SalesPriceVM\":100,\"DiscountPercentVM\":0,\"product_quantity\":2,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=25899\"]","created_at":"2020-05-21 01:43:10","updated_at":"2020-05-21 01:43:10",
"id":"3","user_code":"C020557","name":"PR 1","details":"[\"Id\":13319,\"Name2\":\"EXIUM CAPS 20\",\"SalesPriceVM\":8.5,\"DiscountPercentVM\":0,\"product_quantity\":10,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=13319\",\"Id\":16432,\"Name2\":\"DOMIDON 10MG TAB\",\"SalesPriceVM\":2,\"DiscountPercentVM\":0,\"product_quantity\":10,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=16432\",\"Id\":19480,\"Name2\":\"ALOCAP SOFT CAP\",\"SalesPriceVM\":6,\"DiscountPercentVM\":0,\"product_quantity\":10,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=19480\",\"Id\":23223,\"Name2\":\"CYNTA 100MG TAB.\",\"SalesPriceVM\":25,\"DiscountPercentVM\":0,\"product_quantity\":40,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=23223\",\"Id\":2063,\"Name2\":\"MARLOX PLUS-200 ML-SUSPENSION\",\"SalesPriceVM\":110,\"DiscountPercentVM\":0,\"product_quantity\":1,\"Image_Url\":\"http:\\\/\\\/182.160.118.235:8099\\\/api\\\/ImageFile\\\/GetImage?enumEntityType=2&entityId=2063\"]","created_at":"2020-05-21 07:30:29","updated_at":"2020-05-21 07:30:29",
]

我正在尝试阅读“详细信息”。详细来说,我试图读取三个对象,每个对象的 Id,product_quantity 并将它们插入到 db 表中。

我的代码

public void PostSaveCartItemData()
        
            var contents = File.ReadAllText("saved_carts.json");
            dynamic jsonResponse = JsonConvert.DeserializeObject(contents);
            int i = 0;
            using (MainDataContext ctx = new MainDataContext())
            

                List<SavedCartItem> list = new List<SavedCartItem>();

                foreach (var item in jsonResponse.data)
                
                    var productId = item.details[i].Id;
                    var qty = item.details[i].product_quantity;
                    i++;

                    SavedCartItem entity = new SavedCartItem()
                    
                        ProductId = productId,
                        Qty = qty ,
                        

                    ;

                    list.Add(entity);
                

                ctx.SavedCartItems.AddRange(list);
                   ctx.SaveChanges();
            
        

我们将非常感谢您的帮助

【问题讨论】:

details 属性被 JSOIN 编码为字符串,如果您想使用它,您需要进一步反序列化该值。 【参考方案1】:

details 属性是 JSON 编码的字符串,如果您想使用它,您需要进一步反序列化该值。另外,你应该避免使用dynamic,没有必要。例如:

var jsonResponse = JObject.Parse(Json);

foreach (var item in jsonResponse["data"])

    var detailsJson = item["details"];

    var details = JArray.Parse(detailsJson.ToString());

    foreach(var detail in details)
    
        var productId = detail["Id"];
        var qty = detail["product_quantity"];

        Console.WriteLine(productId);

        Console.WriteLine(qty);
    


【讨论】:

以上是关于“无法访问 Newtonsoft.Json.Linq.JValue 上的子值”的主要内容,如果未能解决你的问题,请参考以下文章