@@ -55,9 +55,84 @@ <h2 class="title" id="slide-0">
5555 < a href ="/json/php/slide.html " class ="do-not-print "> als Präsentation ▻</ a >
5656 </ div >
5757 < div id ="slide-1 " title ="Folie Nr. 1 "> </ div >
58+ < p > Die Befehle < code > json_encode</ code > und < code > json_decode</ code > sind teil der < code > JSON</ code > Extension.</ p >
59+
60+ < p > In < code > phpinfo()</ code > kann man sehen ob diese Extension aktiviert ist:</ p >
61+
62+ < p > < img src ="/images/json/jsonenabled.png " alt ="" > </ p >
63+
5864
5965< a class ="slide_break do-not-print " id ="slide-2 " href ="slide.html#slide-2 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 2 "> ▻</ a >
6066
67+ < h2 id ="json-als-austauschformat "> JSON als Austauschformat</ h2 >
68+
69+ < p > Nicht alles was in JavaScript möglich ist wird in PHP als JSON akzeptiert.</ p >
70+
71+
72+ < a class ="slide_break do-not-print " id ="slide-3 " href ="slide.html#slide-3 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 3 "> ▻</ a >
73+
74+ < h3 id ="anfhrungszeichen "> Anführungszeichen</ h3 >
75+
76+ < p > In Javascript müssen attribute nicht in Anführungszeichen stehen:</ p >
77+
78+ < div class ="example ">
79+ < h4 class ="caption "> Javascript Code < small > OK in Javascript, nicht OK in PHP</ small > </ h4 >
80+ < pre > < code class ="language-javascript linenums "> {
81+ prop1 : "Schokolade",
82+ prop2 : 42,
83+ }
84+ </ code > </ pre > </ div >
85+
86+ < p > Wenn JSON als Austauschformat zwischen Sprachen verwendet wird, dann müssen
87+ alle Attribute in Anführungszeichen stehen:</ p >
88+
89+ < div class ="example ">
90+ < h4 class ="caption "> Javascript Code < small > OK in allen Sprachen</ small > </ h4 >
91+ < pre > < code class ="language-javascript linenums "> {
92+ "prop1" : "Schokolade",
93+ "prop2" : 42,
94+ }
95+ </ code > </ pre > </ div >
96+
97+
98+ < a class ="slide_break do-not-print " id ="slide-4 " href ="slide.html#slide-4 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 4 "> ▻</ a >
99+
100+ < h3 id ="keine-funktionen "> Keine Funktionen</ h3 >
101+
102+ < p > In Javascript kann ein Wert in JSON-Schreibweise auch Funktionen enthalten:</ p >
103+
104+ < div class ="example ">
105+ < h4 class ="caption "> Javascript Code < small > OK in Javascript, nicht OK in PHP</ small > </ h4 >
106+ < pre > < code class ="language-javascript linenums "> {
107+ 'prop1' : 'Schokolade',
108+ 'prop2' : 42,
109+ 'method_1' : function (x) { return x + ' ist schlecht' },
110+ 'method_2' : x => x + ' ist gut'
111+ }
112+ </ code > </ pre > </ div >
113+
114+ < p > Wenn JSON als Austauschformat zwischen Sprachen verwendet wird darf es keine Funktionen enthalten.</ p >
115+
116+ < div class ="example ">
117+ < pre > < code class ="language-php linenums "> $json_string = "{
118+ 'prop1' : 'Schokolade',
119+ 'prop2' : 42,
120+ 'method_1' : function (x) { return x + ' ist schlecht' },
121+ 'method_2' : x => x + ' ist gut'
122+ }";
123+ $data = json_decode($json_string);
124+ if( $data == null ) {
125+ echo json_last_error_msg(); // Output: "Syntax Error"
126+ exit();
127+ }
128+ </ code > </ pre > </ div >
129+
130+ < p > In PHP wird das gesamte JSON-Objekt verworfen, der Rückgabewert ist < code > null</ code >
131+ und < code > json_last_error_msg()</ code > gibt “Syntax Error” zurück.</ p >
132+
133+
134+ < a class ="slide_break do-not-print " id ="slide-5 " href ="slide.html#slide-5 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 5 "> ▻</ a >
135+
61136< h2 id ="serialisierung "> Serialisierung</ h2 >
62137
63138< p > Der PHP Befehl lautet < code > json_encode</ code > .</ p >
@@ -67,9 +142,9 @@ <h2 id="serialisierung">Serialisierung</h2>
67142< p > < img src ="/images/json/mapping-php-to-json.svg " alt ="encode " > </ p >
68143
69144
70- < a class ="slide_break do-not-print " id ="slide-3 " href ="slide.html#slide-3 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 3 "> ▻</ a >
145+ < a class ="slide_break do-not-print " id ="slide-6 " href ="slide.html#slide-6 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 6 "> ▻</ a >
71146
72- < h2 id ="objekt "> Objekt</ h2 >
147+ < h3 id ="objekt "> Objekt</ h3 >
73148
74149< p > Ein PHP Objekt lässt sich als JSON Objekt darstellen:</ p >
75150
@@ -82,9 +157,9 @@ <h2 id="objekt">Objekt</h2>
82157</ code > </ pre > </ div >
83158
84159
85- < a class ="slide_break do-not-print " id ="slide-4 " href ="slide.html#slide-4 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 4 "> ▻</ a >
160+ < a class ="slide_break do-not-print " id ="slide-7 " href ="slide.html#slide-7 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 7 "> ▻</ a >
86161
87- < h2 id ="array "> Array</ h2 >
162+ < h3 id ="array "> Array</ h3 >
88163
89164< p > Ein “normales” Array mit Integer als Index lässt sich
90165direkt auf JSON abbilden:</ p >
@@ -96,9 +171,9 @@ <h2 id="array">Array</h2>
96171</ code > </ pre > </ div >
97172
98173
99- < a class ="slide_break do-not-print " id ="slide-5 " href ="slide.html#slide-5 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 5 "> ▻</ a >
174+ < a class ="slide_break do-not-print " id ="slide-8 " href ="slide.html#slide-8 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 8 "> ▻</ a >
100175
101- < h2 id ="assoziatives-array "> Assoziatives Array</ h2 >
176+ < h3 id ="assoziatives-array "> Assoziatives Array</ h3 >
102177
103178< p > Jedes Array kann in PHP auch Strings als Index enthalten.
104179Ist das der Fall, dann wird es in JSON als Objekt dargestellt:</ p >
@@ -113,7 +188,7 @@ <h2 id="assoziatives-array">Assoziatives Array</h2>
113188in PHP. Deswegen wird es als Objekt dargestellt.</ p >
114189
115190
116- < a class ="slide_break do-not-print " id ="slide-6 " href ="slide.html#slide-6 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 6 "> ▻</ a >
191+ < a class ="slide_break do-not-print " id ="slide-9 " href ="slide.html#slide-9 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 9 "> ▻</ a >
117192
118193< h2 id ="deserialisierung "> Deserialisierung</ h2 >
119194
@@ -124,7 +199,7 @@ <h2 id="deserialisierung">Deserialisierung</h2>
124199< p > Hier gibt es zwei Varianten, die man über das zweite Argumente von < code > json_decode</ code > unterscheidet.</ p >
125200
126201
127- < a class ="slide_break do-not-print " id ="slide-7 " href ="slide.html#slide-7 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 7 "> ▻</ a >
202+ < a class ="slide_break do-not-print " id ="slide-10 " href ="slide.html#slide-10 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 10 "> ▻</ a >
128203
129204< h3 id ="decode-false "> decode false</ h3 >
130205
@@ -144,7 +219,7 @@ <h3 id="decode-false">decode false</h3>
144219</ code > </ pre > </ div >
145220
146221
147- < a class ="slide_break do-not-print " id ="slide-8 " href ="slide.html#slide-8 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 8 "> ▻</ a >
222+ < a class ="slide_break do-not-print " id ="slide-11 " href ="slide.html#slide-11 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 11 "> ▻</ a >
148223
149224< h3 id ="decode-true "> decode true</ h3 >
150225
@@ -164,7 +239,7 @@ <h3 id="decode-true">decode true</h3>
164239</ code > </ pre > </ div >
165240
166241
167- < a class ="slide_break do-not-print " id ="slide-9 " href ="slide.html#slide-9 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 9 "> ▻</ a >
242+ < a class ="slide_break do-not-print " id ="slide-12 " href ="slide.html#slide-12 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 12 "> ▻</ a >
168243
169244< h2 id ="json-als-rckgabewert "> JSON als Rückgabewert</ h2 >
170245
@@ -177,7 +252,7 @@ <h2 id="json-als-rckgabewert">JSON als Rückgabewert</h2>
177252</ code > </ pre > </ div >
178253
179254
180- < a class ="slide_break do-not-print " id ="slide-10 " href ="slide.html#slide-10 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 10 "> ▻</ a >
255+ < a class ="slide_break do-not-print " id ="slide-13 " href ="slide.html#slide-13 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 13 "> ▻</ a >
181256
182257< h2 id ="aus-einer-json-datei "> Aus einer JSON Datei</ h2 >
183258
@@ -193,7 +268,7 @@ <h2 id="aus-einer-json-datei">Aus einer JSON Datei</h2>
193268</ code > </ pre > </ div >
194269
195270
196- < a class ="slide_break do-not-print " id ="slide-11 " href ="slide.html#slide-11 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 11 "> ▻</ a >
271+ < a class ="slide_break do-not-print " id ="slide-14 " href ="slide.html#slide-14 " title ="Wechsle zur Präsentations-Ansicht, Folie Nr. 14 "> ▻</ a >
197272
198273< h2 id ="referenz "> Referenz</ h2 >
199274
0 commit comments