[英]Convert the string containing array and dict into the list and vice versa in c#
我們如何在 c# 中將以下字符串轉換為列表,反之亦然?
String1: "['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]"
Expected List1: ['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]
預期成績:
Console.WriteLine(List1[0]) //Output: 'hello'
Console.WriteLine(List1[4][2][1]) //Output: 4
Console.WriteLine(List1[4][2][2]['key']) //Output: 'value'
筆記:
在python 中,我們有一個eval(expression)方法可以將字符串轉換為列表。
我們有一個repr(obj)方法將列表轉換回字符串。
請使用python方法參考以下示例代碼。 我需要在 c# 中使用類似的方法。
>>> str1="['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]"
>>> list1=list(eval(str1))
>>> list1
['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]
>>> list1[4][2][2]['key']
'value'
>>> list1[4][2][0]
'3'
>>> str2=repr(list1)
>>> str2
"['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]"
每一個解決方案都將不勝感激。
謝謝!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.