Question

In: Computer Science

I need some information on how to show the code to create a JSON data structure...

I need some information on how to show the code to create a JSON data structure with food, calories, fat, protein. while using PHP.

as well as be able to add an additional value through a text box if possible. The program only needs to read the json data in program and show output in the console

I would like the data to be shown in a table once its been compiled.

Solutions

Expert Solution

PHP Program to show JSON Data and update JSON Data.

please refer to the screenshot of the code to understand code indentation.

<?php
/* 
$json is a json data we have
*/
$json = '[{"food":"Food 1","calories":"54","fat":"45","protein":"54"},
{"food":"Food 2","calories":"65","fat":"66","protein":"5"},
{"food":"Food 3","calories":"56","fat":"0","protein":"0"}]';

/*
Following code line print each data present in $json
*/

//First decode json data so that it can be converted to associative array
$decoded_json  = json_decode($json,true);
echo "Original Record<br>";
for($i=0;$i<sizeof($decoded_json);$i++) {
     echo "Displaying record: ".($i+1)."<br><br>"; 
     echo "Food is :".$decoded_json[$i]['food']."<br>"; //Getting value of food key at index $i
     echo "Calories :".$decoded_json[$i]['calories']."<br>"; //Getting value of calories key at index $i
     echo "Fat : ".$decoded_json[$i]['fat']."<br>"; //Getting value of fat key at index $i
     echo "Protein : ".$decoded_json[$i]['protein']."<br>"; //Getting value of protein key at index $i
}

/*
Adding record to JSON.
*/

/*
Create a new array element and add key value 
here key will be same as json key and value will be any of your choice
make sure to create same keys as in json element.
*/
$new_record = array();
$new_record['food'] = "Food 4";
$new_record['calories'] = "Food 4";
$new_record['fat'] = "Food 4";
$new_record['protein'] = "Food 4";

/*
append json with newly created associative array using array_push() 
*/
array_push($decoded_json,$new_record);

/*
Print new json record.
*/

echo "<br>After Adding 1 element<br><br>";
for($i=0;$i<sizeof($decoded_json);$i++) {
     echo "Displaying record: ".($i+1)."<br><br>"; 
     echo "Food is :".$decoded_json[$i]['food']."<br>"; //Getting value of food key at index $i
     echo "Calories :".$decoded_json[$i]['calories']."<br>"; //Getting value of calories key at index $i
     echo "Fat : ".$decoded_json[$i]['fat']."<br>"; //Getting value of fat key at index $i
     echo "Protein : ".$decoded_json[$i]['protein']."<br>"; //Getting value of protein key at index $i
}

?>

Screenshot of code:

Sample Output:


Related Solutions

C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json"...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json" i need to upload a file, three strings. It needs to be in Java, but i keep getting issues.
Use a JSON data structure that contains student information (StudentId, FName, LName, Address, City, State, Zip,...
Use a JSON data structure that contains student information (StudentId, FName, LName, Address, City, State, Zip, E-mail, Phone, Major, AdvisorName, AdvisorEmail). Using the Foreach statement, display all the contents of the JSON data structure into a webpage. What to turn in: 1. Screenshots: Code, JSON data, Web page showing JSON data in a table - pulled using the Foreach instruction.
This is for Accounting for Managers course. I need to create a presentation that will show...
This is for Accounting for Managers course. I need to create a presentation that will show a company that was not doing well or was in crisis before but later it completely turnaround the situation and started making lot of profit. In this case i need to use current as well as few previous years company data to show these crisis to turnaround journey(so please inlude the money sheets as well in the ppt). Please include some basic accounting concepts...
I need to create a code in C++ that first has a menu animation of game...
I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
Here is an outline for some code I need to write. This class used Intro to...
Here is an outline for some code I need to write. This class used Intro to Java Edition 11, and we only got to Chapter 9. There is no set right way that this program has to be done. Feel free to interpret this in a way that you wish. Mrs. Quackenbush is back! She now has bought a beverage establishment, The Green Dragon Inn, and needs to have a way to insure the consistency of the drinks her employees...
show some example of few recursive problem in java language and the solution code. Need more...
show some example of few recursive problem in java language and the solution code. Need more practice as a new learner
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment...
In this assignment, you will create a well-formed JSON object which encapsulates the information specified. Alignment This assignment aligns with the following objectives: Identify the differences between these three modes of interacting with data, i.e. web forms, programs, and APIs Recognize well-formed JSON objects Parse JSON objects Details Imagine that using the API provided by a service like Yelp (let's call it YelpYou) you have extracted information about Sichuan Dragon, which has three branches (restaurants) in New York City. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT