JSON反序列化错误

时间:2018-02-17 18:07:01

标签: c# json asp.net-mvc-5

All ..我有一个简单的JSON文件

{
"code": 0,
"message": "success",
"items": [
    {
        "item_id": "1186247000000062086",
        "name": "18 Holes",
        "unit": "Each",
        "status": "active",
        "source": "user",
        "is_linked_with_zohocrm": false,
        "zcrm_product_id": "",
        "description": "",
        "rate": 25,
        "tax_id": "",
        "tax_name": "",
        "tax_percentage": 0,
        "purchase_description": "",
        "purchase_rate": 0,
        "is_combo_product": false,
        "item_type": "inventory",
        "product_type": "goods",
        "stock_on_hand": 10000000,
        "available_stock": 10000000,
        "actual_available_stock": 10000000,
        "sku": "1",
        "upc": "",
        "ean": "",
        "isbn": "",
        "part_number": "",
        "reorder_level": 0,
        "image_name": "",
        "image_type": "",
        "created_time": "2018-02-17T08:13:25-0800",
        "last_modified_time": "2018-02-17T08:13:25-0800"
    }
],
"page_context": {
    "page": 1,
    "per_page": 200,
    "has_more_page": false,
    "report_name": "Items",
    "applied_filter": "Status.All",
    "custom_fields": [],
    "sort_column": "name",
    "sort_order": "A"
}

}

我已使用以下代码将其放入dd框

var Products = JsonConvert.DeserializeObject<dynamic>(responseString.ToString());
List<SelectListItem> Product_List = new List<SelectListItem>();

foreach (var item in Products)
{
    Product_List.Add(new SelectListItem() { Text = item.items.name, Value = "1" });        
}

ViewBag.Product = Product_List;

return View();

似乎很简单,但我一直收到以下错误:  'Newtonsoft.Json.Linq.JProperty'不包含'items'的定义

我以完全相同的方式使用它来获得成功的不同JSON记录......任何想法?

1 个答案:

答案 0 :(得分:0)

您正在尝试导航到不是json成员的属性。在将项目添加到选择列表时,尝试使用此 Text = item.name 替换 Text = item.items.name