如何在ASP.NET c#的下拉列表中显示JSON数组数据?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (85)

我想在ASP.NET的下拉列表中填充JSON数组数据。我的JSON阵列是:

"Product": [
            {
                "id": 0,
                "productId": 0,
                "quantity": 20,
                "productName": "Rawai",
                "brandName": "Kamal Products",
                "productCode": 0,
                "brandId": 0,
                "productPrice": 0
            },
  {
                "id": 0,
                "productId": 0,
                "quantity": 22,
                "productName": "OIL",
                "brandName": "Kamal Proucts",
                "productCode": 0,
                "brandId": 0,
                "productPrice": 0
            },
 {
                "id": 0,
                "productId": 0,
                "quantity": 3,
                "productName": "Ghee",
                "brandName": "Kamal Products",
                "productCode": 0,
                "brandId": 0,
                "productPrice": 0
            }

        ]
 Product lstProduct = new Product();                 List lstBrandProduct = new List();
            string urls = new System.Net.WebClient().DownloadString(urlnames);
            JArray jsonArrays = JArray.Parse(urls);
            for (int i = 0; i < jsonArrays.Count; i++)
            {
                CustomerOrder isiData = (new JavaScriptSerializer()).Deserialize<CustomerOrder>(jsonArrays[i].ToString());

                for (int j = 0; j < isiData.product.Count; j++)
                {
                    drpListBrand.DataTextField = isiData.product[j].BrandName;
                }
            }
提问于
用户回答回答于

你可以使用NewtonSoft.JSON Nuget

var files = JObject.Parse(YourJson);
var recList = files.SelectTokens("$").ToList();
  foreach (JProperty item in recList.Children())
  {
     var key = item.Name.ToString(); //store the root item
     var value= item.Value.ToString();
     var breakitem = item.Values().ToList();
     for (int i = 0; i < breakitem.Count; i++)
     {
      string drpvalue = breakitem[i]["productName"].ToString();
      //ADD this value to your dropdownList Object
     }     
   }

扫码关注云+社区

领取腾讯云代金券