How to convert a string to an array with python?
I need to convert the value of items to an array.
I tried using json.loads, but it doesn't work.
Only the value of the ITEMS key is in string and it is exactly this value that I need to convert to array
What am I doing wrong?
Thank you.
"metadata": {
"deleted": "false",
"low_manual": "false",
"canceled": "false",
"items": "[{'descricao': 'ITEM1', 'amount': '200'}, {'amount': '3000', 'descricao': 'ITEm2'}]"
}
What you're trying to do is called "deserialization". Deserialization is the process of converting from JSON text to an object (in this case Python-Object). Serialization and Deserialization are best done with a library.
Deserialization Python tool:
What you want to do is install the library and import it into your project. Then, you want to conduct a deserialization of the JSON text by using a command similar to the following:
obj = jsonpickle.decode(JSON text)
"obj" is now an object with the attributes of "deleted","low_manual", "canceled", and "items". I believe "items" will contain an array of objects which you can access through the parent, "obj".
Resolved.
var = "metadata": {
"deleted": "false",
"low_manual": "false",
"canceled": "false",
"items": "[{'descricao': 'ITEM1', 'amount': '200'}, {'amount': '3000', 'descricao': 'ITEm2'}]"
}
from ast import literal_eval
stringC = literal_eval(var['metadata']['items'])
stringC[0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.