Convert the string containing array and dict into the list and vice versa in c#

user6677473

How can we convert the following string into a list and vice versa in 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'}]]]

Expected results:

Console.WriteLine(List1[0])               //Output: 'hello'

Console.WriteLine(List1[4][2][1])         //Output:  4

Console.WriteLine(List1[4][2][2]['key'])  //Output: 'value'

Note:

In python, we have an eval(expression) method to convert the string into a list.

we have a repr(obj) method to convert the list back into a string.

Please refer to the below sample code using python methods. I require similar methods in 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'}]]]"

Each and every solution will be appreciated.

Thanks!!!

OfirD

You can use Newtonsoft.Json library to achieve that. You need to deserialize the string as a JArray, as follows:

public static void Main()
{
    var arr = "['hello', 'world', 100, 12.59, [1, '2', ['3', 4, {'key': 'value'}]]]";
    var parsed = JsonConvert.DeserializeObject<JArray>(arr);
    Console.WriteLine(parsed[4][2][2]["key"]);
}

You can see a live demo here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert List<int> to string and vice versa?

How to convert numpy array to string and vice versa

How to convert byte array to string and vice versa?

How to convert a string to hex and vice versa in c?

Java: Can I convert List of Object to List of String[] and vice versa?

Cannot convert String to byte array and vice versa in Java

How do you convert a byte array to a hexadecimal string, and vice versa?

Convert ListView to Line separated String Array and vice versa

How to convert string into numpy array for tensorflow and vice versa

How to convert WCHAR* to string in C++ and vice versa?

How to convert String[] to String and vice versa in Android

How to convert a netty ByteBuf to a String and vice versa

how to convert joda datetime to String and vice versa

Python convert set to string and vice versa

Convert an IP string to a number and vice versa

convert string to json in golang and vice versa?

Convert string time to unix time and vice versa

Convert a Guid string into BigInteger and vice versa

Convert struct array to cell of structs and vice versa

Convert a byte array to integer in Java and vice versa

Convert file to byte array and vice versa

How to convert a float into a byte array and vice versa?

ByteBuffer - convert long to char array and vice versa

Android-Java List to String and vice versa

check a string array from a list contains a string from another list or vice versa

Converting String to Byte Array and vice versa

F# efficient way to convert Map into Dict (and vice versa)

How to convert string containing list of dictionaries to python (object) LIST of DICT

How to convert an integer to a c-string and vice versa and other datatypes without using any in-built functions?

TOP Ranking

HotTag

Archive