0

How can i pull the "Toppings" Values from my JSON string into my list items? Thanks for your help!

<html>
<body>

<section>
<h2>Toppings</h2>
<ul>
    <li>JSON String Value </li>
    <li>JSON String Value </li>
    <li>JSON String Value </li>
    <li>JSON String Value</li>
</ul>
</section>

     </body>
<script>
var myObj ={"menu": {"slice of pizza": "2.00", "toppings": {"pepperoni": ".25","meatballs": ".35", "mushrooms": ".40","olives": ".20"},"sides": {"potato salad": "1.25","hummus": "2.50","caesar salad": "3.50","garden salad": "2.25"},   "drinks": { "soda": {   "small": "1.95",  "medium": "2.20","large": "2.50" }, "juice": "2.00", "water": "1.25"}}};
var myJSON = JSON.stringify(myObj);
</script>

</html>
2
  • Please include attempted solutions, why they didn't work, and the expected results. That would really help us to figure out the issue with your code. Commented Mar 6, 2018 at 15:27
  • Why you stringify your JSON? Commented Mar 6, 2018 at 15:27

5 Answers 5

1

Here goes your code solution

use this code

<html>
<body>

<section>
<h2>Toppings</h2>
<ul id="serveJson">
</ul>
</section>

     </body>
<script>
var myObj ={"menu": {"slice of pizza": "2.00", "toppings": {"pepperoni": ".25","meatballs": ".35", "mushrooms": ".40","olives": ".20"},"sides": {"potato salad": "1.25","hummus": "2.50","caesar salad": "3.50","garden salad": "2.25"},   "drinks": { "soda": {   "small": "1.95",  "medium": "2.20","large": "2.50" }, "juice": "2.00", "water": "1.25"}}};

var toppings = myObj.menu.toppings;

var ul = document.getElementById('serveJson');
for(name in toppings)
{
    var li = document.createElement('li');
    li.appendChild( document.createTextNode(toppings[name]) );
    ul.appendChild(li);
}

</script>

</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Use Object.Keys to grab the keys of Toppings, then iterate those with a forEach to then populate your unlinked list with list items like so, this is assuming you give your ul node an id property:

let myToppings = Object.Keys(myObj.menu.toppings);
let myUl = document.getElementById("yourUlId");

myToppings.forEach(function(key) {
       let liItem = document.createElement("LI");
       let textnode = document.createTextNode(key); 
       liItem.appendChild(textnode);
       myUl.appendChild(liItem);
});

Comments

1

Assume you have already li defined. Then you can try the following way:

var myObj ={"menu": {"slice of pizza": "2.00", "toppings": {"pepperoni": ".25","meatballs": ".35", "mushrooms": ".40","olives": ".20"},"sides": {"potato salad": "1.25","hummus": "2.50","caesar salad": "3.50","garden salad": "2.25"},   "drinks": { "soda": {   "small": "1.95",  "medium": "2.20","large": "2.50" }, "juice": "2.00", "water": "1.25"}}};

var myJSON = Object.keys(myObj.menu.toppings);
var allLI = document.querySelectorAll('ul > li');
allLI.forEach(function(li, i){
  li.textContent = myJSON[i] + ': ' + myObj.menu.toppings[myJSON[i]];
});
<section>
<h2>Toppings</h2>
<ul>
    <li>JSON String Value </li>
    <li>JSON String Value </li>
    <li>JSON String Value </li>
    <li>JSON String Value</li>
</ul>
</section>

Comments

1

My solution :)

var ul = document.getElementsByTagName('ul')[0]; //Get the <ul> to append toppings
var myObj ={"menu": {"slice of pizza": "2.00", "toppings": {"pepperoni": ".25","meatballs": ".35", "mushrooms": ".40","olives": ".20"},"sides": {"potato salad": "1.25","hummus": "2.50","caesar salad": "3.50","garden salad": "2.25"},   "drinks": { "soda": {   "small": "1.95",  "medium": "2.20","large": "2.50" }, "juice": "2.00", "water": "1.25"}}};

Object.keys(myObj.menu.toppings).forEach((key) => { //for each topping create a new li and append to the ul
    var li = document.createElement('li');
    li.innerText = `${key}: ${myObj.menu.toppings[key]}`;
    ul.append(li);
});

Result:

<ul>
    <li>pepperoni: .25</li>
    <li>meatballs: .35</li>
    <li>mushrooms: .40</li>
    <li>olives: .20</li>
</ul>

Comments

0

Try this

<!DOCTYPE html>
    <html>
    <body>

    <h2>Convert a JavaScript object into a JSON string, and send it to the server.</h2>

    <script>

    var myObj = {"menu": {"slice of pizza": "2.00", "toppings": {"pepperoni": ".25","meatballs": ".35", "mushrooms": ".40","olives": ".20"},"sides": {"potato salad": "1.25","hummus": "2.50","caesar salad": "3.50","garden salad": "2.25"},   "drinks": { "soda": {   "small": "1.95",  "medium": "2.20","large": "2.50" }, "juice": "2.00", "water": "1.25"}}};
    var myj=JSON.parse(JSON.stringify(myObj));
    console.log(myj.menu.toppings);


    </script>

    </body>
    </html>

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.