0

I'm stucked with some problem. I've sent int array from JS to Python via AJAX and it has been converted to JSON (as I used JSON.stringify()), so now it's a string "[1,2,3,4,5]". How can I convert it in Python back to int array [1,2,3,4,5]? I've tried to convert this array to numpy array np.asarray(features_user, dtype="int"), but this not helped

2
  • 1
    You can call json.loads on that string to convert it into a python list. Commented Jun 5, 2018 at 21:00
  • @chrisz thank you so much! You saved my time, as I`m making diploma project. Commented Jun 5, 2018 at 21:09

1 Answer 1

1

json.loads() is what you're looking for. make sure you add the s in load. the s stands for string.

So json.load is for a file, json.loads for a string

>>> import json
>>> a = "[1,2,3,4,5]"
>>> b = json.loads(a)
>>> b,type(b)
([1, 2, 3, 4, 5] , <class 'list'>)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.