usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf ·...

20
usingJSON.notebook 1 October 26, 2013 Explanation of var_dump which is a php function I am using to display structured information like the type and value you see displayed. You will see this in some of the examples that follow.

Transcript of usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf ·...

Page 1: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

1

October 26, 2013

Explanation of var_dump which is a php functionI am using to display structured information like thetype and value you see displayed.You will see this in some of the examples that follow.

Page 2: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

2

October 26, 2013

I started out by experimenting with the decoding json.

Page 3: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

3

October 26, 2013

It will take two slides to show the codefor this program.  This is the first studentin the JSON file.  Notice that students containsname, major, yrenrolled, academic which isbroken down into numcr and gpa and phone whichis broken down into type and num.

Notice that the JSON is in singlequotes (since I used double quotesinternally) and assigned to $info.

Page 4: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

4

October 26, 2013

I have a phpvariable called$students will be assigned the decoded $info.

I could dump the content ofstudents using var_dump.

When I echo the literal name, I concatenate it to$students which contains the json_decode of $info.I then look at what is in $students and take the first student which is done with students[0] and the nameassociated with it. The next line has the major associated with it. Now to do academic I need to have$students­>students[0]­>academic­>numcrs to go to academics and the numcrs for that student beneath.

Notice that academics had one occurance of numcr and gpa but phone has three sets beneath it, each having a type and a num.So...$students­>students[0]­>phone[0]­>type will get the first phone for the first student.  To get the other phones, I use phone[1] and phone[2].

Page 5: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

5

October 26, 2013

Here I did the var_dump instead of commenting it out.  More of the program on the next slide.

Page 6: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

6

October 26, 2013

I decided to assign the json_decode of $infoto $thestudents to clarify.Here I have a loop that goes through anddisplays both records.Note that I now have $ct in the square bracketts.$thestudents­>students[$ct]­>name

Page 7: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

7

October 26, 2013

Page 8: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

8

October 26, 2013

I check to see if there was data and then I useda for statement to loop through while $i was < countof the students.  Back to using $students here ­ guess you can tell the order that I wrote them in.

This does a similar check on phones.

Page 9: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

9

October 26, 2013

Page 10: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

10

October 26, 2013

Now I am using the php foreach whereI will process with $student for eachindividual student.  $students­>studentsis being processed but each recorddeals with $student so I see$student­>name.

Page 11: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

11

October 26, 2013

Page 12: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

12

October 26, 2013

The change isthe table format.

Page 13: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

13

October 26, 2013

     {"students":[         {          "name": "Susan French",          "major": "Webmaster",          "yrenrolled": 2010,          "academic":               {               "numcr": 54,               "gpa": 3.7              }         },         {          "name": "Jay Costa",          "major": "Programming",          "yrenrolled": 2011,          "academic":               {               "numcr": 49,               "gpa": 3.6              }         },         {          "name": "Susan Ash",          "major": "Programming",          "yrenrolled": 2010,          "academic":               {               "numcr": 36,               "gpa": 3.8              }         }       ]}

This is the external file named stufile.json

Page 14: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

14

October 26, 2013

First I extracted a namein JavaScript usingjQuery to get the JSONfile.Then I moved to php todo the same thing.This time I usedfile_get_contents toget the PHP file andthen I decoded it foruse.

Page 15: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

15

October 26, 2013

This is an example of encoding.  I have an array that I did in PHP and I am doing a json_encode of the array.  The results are in JSON.

Page 16: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

16

October 26, 2013

Page 17: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

17

October 26, 2013

Some experimenting (I got carriedaway) with arrays.

Page 18: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

18

October 26, 2013

I went to a JSON viewer at the this address:http://jsonviewer.stack.huThen I entered in the address under Load JSON data and the new address reads:http://jsonviewer.stack.hu/#http://cisweb.bristolcc.edu/~pgrocer/JSON/test2013/stufile.json

{  "students": [    {      "name": "Susan French",      "major": "Webmaster",      "yrenrolled": 2010,      "academic": {        "numcr": 54,        "gpa": 3.7      }    },    {      "name": "Jay Costa",      "major": "Programming",      "yrenrolled": 2011,      "academic": {        "numcr": 49,        "gpa": 3.6      }    },    {      "name": "Susan Ash",      "major": "Programming",      "yrenrolled": 2010,      "academic": {        "numcr": 36,        "gpa": 3.8      }    }  ]}

Page 19: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

19

October 26, 2013

Page 20: usingJSON.notebook October 26, 2013cisweb.bristolcc.edu/~pgrocer/JSON/test2013/usingJSON.pdf · usingJSON.notebook 3 October 26, 2013 It will take two slides to show the code for

usingJSON.notebook

20

October 26, 2013

I went to jsonlint.comto validate the JSONin stufile.JSON

I went to jsonlint.comto validate the JSONin stufile.JSON