如何从列表中的字典设置/检索值?

时间:2019-07-02 03:13:23

标签: c# list dictionary object escaping

所以我能够检索elements.handle,但无法检索elements.handle〜。有什么可能的方法吗?

我尝试过转义字符元素['handle \〜'],但似乎不起作用

{"elements":[{"handle":"urn:li:emailAddres:7440721026","handle~":{"emailAddress":"abc@gmail.com"}}]}

1 个答案:

答案 0 :(得分:1)

由于您要查找的值(abc@gmail.com)嵌套了几层。如果要检索它,则需要相应地访问它。

var data = "{\"elements\":[{\"handle\":\"urn:li:emailAddres:7440721026\",\"handle~\":{\"emailAddress\":\"abc@gmail.com\"}}]}";

dynamic x = JsonConvert.DeserializeObject(data);

// x is an object with one property: elements
Console.WriteLine($"x: {x}");

// x.elements is a List (based on your question)
Console.WriteLine($"x.elements: {x.elements}");

// x.elements[0] is a Dictionary (based on your question)
Console.WriteLine($"x.elements[0]: {x.elements[0]}");

// x.elements[0]["handle~"] is another Dictionary
Console.WriteLine($"x.elements[0][handle~]: {x.elements[0]["handle~"]}");

// x.elements[0]["handle~"]["emailAddress"] is the value you want
Console.WriteLine($"x.elements[0][handle~][emailAddress]: {x.elements[0]["handle~"]["emailAddress"]}");

这意味着x.elements[0]["handle~"]["emailAddress"]应该会给您abc@gmail.com