For starters, look at this:
json structure: {"Class A", "Class B"}
Your JSON follows a wrong syntax! See:
Test your Jsons with Jsonlint!
A quick explanation:
{}
(keys) for objects. Therefore, you need to enter the property:
{ "nome" : "LipESprY" }
[]
(brackets) for vectors (list). Therefore, we do not declare the property:
["Valor 1", "Valor 2", "Valor 3"]
If we have more than one object, then we should put them inside a vector (list):
[{
"nome": "LipESprY"
}, {
"nome": "Larissa"
}]
Your JSON fixed, it looks like this:
["Turma A", "Turma B"]
Obviously you can improve your JSON a lot. But it is not the goal of my answer to "improve" what does not refer to the problem. I only mentioned this problem due to need to fix it.
Now to popular the data, can do so:
<!DOCTYPE html>
<html>
<head>
<title>Popular checkbox por LipESprY</title>
<!-- <script type="text/javascript" src="jquery-3.3.1.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<label id="lb_checkbox">
</label>
</body>
<script type="text/javascript">
data = JSON.parse('["Turma A", "Turma B"]');
for (let valor of data) {
$("<input type='checkbox' value='"+valor+"'>"+valor+"<br>").appendTo('#lb_checkbox');
}
</script>
</html>
That’s what I was able to develop with the little code you posted, after a lot of effort! Now it’s up to you to adapt to your project...
Larissa, there’s little code there! You can’t run/simulate your code. Do you want to be popular with data coming via ajax or directly in the rendering you’re doing with PHP? Post the rest of the code...
– LipESprY
It is not recommended to use the comment to implement your question. To do so, edit your question and add the information you want to complement. Remember this information I mentioned.
– LipESprY
data comes in array form by ajax
– Larissa silva
What is the structure of this JSON? Where do you want to "fit" the checkboxes? Inside the
label
? In your script it goes into the input. It’s wrong! See: appendTo() / Another thing, you’ve done one Tour? Another recommended reading: Inquiry - help center– LipESprY
Edit the question, heart!
– LipESprY