在c#中从Dictionary获取值

时间:2016-10-14 07:32:56

标签: c# dictionary

我正试图从c#字典中获取街道,但它无效(收到一些错误)

string data_scope_api = '[
  {

    "retailerName": "vv",
    "name": null,
    "address": {
      "fullShopName": "vv",
      "street": "vv",

    },

  },
  {

    "retailerName": "vv",
    "name": null,
    "address": {
      "fullShopName": "vv",
      "street": "vv"
    },

  }
]'






        dynamic results = JsonConvert.DeserializeObject<dynamic>(data_scope_api);
        foreach (var result in results) {

            string address = result.address;
            Console.Write (address);
            foreach (var address_1 in address) {
                string store_street = address_1.street;
                Console.Write (store_street);
            }
        }

错误

Program.cs(37,37): Error CS1061: Type `char' does not contain a definition for `street' and no extension method `street' of type `char' could be found. Are you missing an assembly reference? (CS1061) (test)

如何修复并使该程序有效?

1 个答案:

答案 0 :(得分:1)

您将地址转换为字符串变量。请改用动态。

foreach (var result in results)
{
    dynamic address = result.address; // address is dynamic address json
    Console.Write(address);

    string store_street = address.street; // street is string data
    Console.Write(store_street);
}