77
To parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
12
This is used to convert string which is array into pure array
var a = '[Aakash,akash]'
a.replace(/\[|\]/g,'').split(',')
(2) ["Aakash", "akash"]
- [Django]-Error: No module named staticfiles
- [Django]-Django – query filter on manytomany is empty
- [Django]-How to set the default of a JSONField to empty list in Django and django-jsonfield?
11
Your response is of the form
services = "['service1', 'service2', 'service3']"
JSON.parse() will work if the parent quote is a single quote and all other child quotes are double quotes. I have quoted an example below
services = '["service1", "service2", "service3"]'
We can use replace() to achieve this
Here is a working example
services = "['service1', 'service2', 'service3']"
services = services.replace(/'/g, '"') //replacing all ' with "
services = JSON.parse(services)
console.log(services)
- [Django]-A good way to redirect with a POST request?
- [Django]-Cron and virtualenv
- [Django]-Django – getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
3
To ensure correct parsing between Django and some javascript browser code, be sure that you return a JsonResponse() in your controller. This ensures you can parse it with JSON.parse() in your Javascript.
- [Django]-Django – how to make ImageField/FileField optional?
- [Django]-Django template can't see CSS files
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
2
Try Using:
var services = "['service1', 'service2', 'service3']"
services = services .split(",");
services [0] = services [0].substring(1);
services [services .length - 1] = services [services .length - 1].substring(
0,
services [services .length - 1].length - 1
);
services .forEach((x, i) => {
services [i] = services [i].includes('"') ? services [i].replaceAll('"', "").trim()
: services [i].replaceAll("'", "").trim();
});
console.log(services );
- [Django]-TypeError: login() takes 1 positional argument but 2 were given
- [Django]-Changing a project name in django
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
1
I recently found a roundabout way of solving a similar problem. I wanted to store an array as a data attribute in an HTML element so that I could use it later in the JS. However, such an HTML attribute could only be stored as a string. To solve this…
- First I turned the arrays into a single string, with each value separated by a comma (ex.
["test 1", "test 2", "test 3"]
is written as“test 1,test 2,test 3”
). - Next, I entered the new string into the desired HTML data attribute. (ex.
<div data-somedata="test 1,test 2,test 3"></div>
) - Upon entering the JS, I retrieved that data string and converted it back into an array by using jQuery’s "split” method. For this check out the snippet here.
let stillString = $("[data-something]").attr('data-something');
console.log(stillString);
let nowArray = stillString.split(",");
console.log(nowArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-something="test 1,test 2,test 3"></div>
This may not look pretty, keeping an array as a string, but it gets the job done and your average users won’t know the difference.
NOTE: You don’t HAVE to use commas. You could use any character really. The only thing that matters that none of the values that you’re trying to separate contain the character that you chose split them with.
- [Django]-How to print line breaks in Python Django template
- [Django]-How to get Django and ReactJS to work together?
- [Django]-What is an efficient way of inserting thousands of records into an SQLite table using Django?
1
Just follow these steps
- let arr = new Array(services);
- let servicesArray = JSON.parse(arr[0]);
By these two steps you array "[‘service1’, ‘service2’, ‘service3’]" would be converted to this one [‘service1’, ‘service2’, ‘service3’]
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-Django gives Bad Request (400) when DEBUG = False
- [Django]-Why second user login redirects me to /accounts/profile/ url in Django?