2021年5月1日星期六

How to convert a string representation of an array of objects to an array of objects? [duplicate]

To convert an array of objects to a string, I can use str(arr).
Reversely, is there a built-in Python function to convert a string representation of an array of object to an array of objects?

In other words, how to convert "[{'key1': 'val1', 'key2': 'val2'}]" back to an array of objects? I thought of doing it using split but it wouldn't be clean like str().

// examples  arr = [{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}, {'key1': 'val1', 'key2': 'val2'}]  arrStr = str(arr)  // "[{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}, {'key1': 'val1', 'key2': 'val2'}]"    arr2 = [{'key1': 'val1', 'key2': 'val2'}]  arrStr2 = str(arr)  // "[{'key1': 'val1', 'key2': 'val2'}]"  
https://stackoverflow.com/questions/67352289/how-to-convert-a-string-representation-of-an-array-of-objects-to-an-array-of-obj May 02, 2021 at 09:37AM

没有评论:

发表评论