Skip to content

Commit 8f65249

Browse files
author
GitLab CI
committed
was ist in json erlabut für php
1 parent f4c5c2e commit 8f65249

2 files changed

Lines changed: 165 additions & 15 deletions

File tree

json/php/index.html

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 : &quot;Schokolade&quot;,
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+
&quot;prop1&quot; : &quot;Schokolade&quot;,
93+
&quot;prop2&quot; : 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+
&#39;prop1&#39; : &#39;Schokolade&#39;,
108+
&#39;prop2&#39; : 42,
109+
&#39;method_1&#39; : function (x) { return x + &#39; ist schlecht&#39; },
110+
&#39;method_2&#39; : x =&gt; x + &#39; ist gut&#39;
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 = &quot;{
118+
&#39;prop1&#39; : &#39;Schokolade&#39;,
119+
&#39;prop2&#39; : 42,
120+
&#39;method_1&#39; : function (x) { return x + &#39; ist schlecht&#39; },
121+
&#39;method_2&#39; : x =&gt; x + &#39; ist gut&#39;
122+
}&quot;;
123+
$data = json_decode($json_string);
124+
if( $data == null ) {
125+
echo json_last_error_msg(); // Output: &quot;Syntax Error&quot;
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
90165
direkt 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.
104179
Ist das der Fall, dann wird es in JSON als Objekt dargestellt:</p>
@@ -113,7 +188,7 @@ <h2 id="assoziatives-array">Assoziatives Array</h2>
113188
in 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

json/php/slide.html

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,81 @@ <h1>JSON und PHP</h1>
8787
</p>
8888
</div>
8989
<div class="slide">
90+
<p>Die Befehle <code>json_encode</code> und <code>json_decode</code> sind teil der <code>JSON</code> Extension.</p>
91+
92+
<p>In <code>phpinfo()</code> kann man sehen ob diese Extension aktiviert ist:</p>
93+
94+
<p><img src="/images/json/jsonenabled.png" alt="" ></p>
95+
96+
</div>
97+
<div class="slide">
98+
99+
<h2 id="json-als-austauschformat">JSON als Austauschformat</h2>
100+
101+
<p>Nicht alles was in JavaScript möglich ist wird in PHP als JSON akzeptiert.</p>
102+
103+
</div>
104+
<div class="slide">
105+
106+
<h3 id="anfhrungszeichen">Anführungszeichen</h3>
107+
108+
<p>In Javascript müssen attribute nicht in Anführungszeichen stehen:</p>
109+
110+
<div class="example">
111+
<h4 class="caption">Javascript Code <small>OK in Javascript, nicht OK in PHP</small></h4>
112+
<pre><code class="language-javascript linenums"> {
113+
prop1 : &quot;Schokolade&quot;,
114+
prop2 : 42,
115+
}
116+
</code></pre></div>
117+
118+
<p>Wenn JSON als Austauschformat zwischen Sprachen verwendet wird, dann müssen
119+
alle Attribute in Anführungszeichen stehen:</p>
120+
121+
<div class="example">
122+
<h4 class="caption">Javascript Code <small>OK in allen Sprachen</small></h4>
123+
<pre><code class="language-javascript linenums"> {
124+
&quot;prop1&quot; : &quot;Schokolade&quot;,
125+
&quot;prop2&quot; : 42,
126+
}
127+
</code></pre></div>
128+
129+
</div>
130+
<div class="slide">
131+
132+
<h3 id="keine-funktionen">Keine Funktionen</h3>
133+
134+
<p>In Javascript kann ein Wert in JSON-Schreibweise auch Funktionen enthalten:</p>
135+
136+
<div class="example">
137+
<h4 class="caption">Javascript Code <small>OK in Javascript, nicht OK in PHP</small></h4>
138+
<pre><code class="language-javascript linenums"> {
139+
&#39;prop1&#39; : &#39;Schokolade&#39;,
140+
&#39;prop2&#39; : 42,
141+
&#39;method_1&#39; : function (x) { return x + &#39; ist schlecht&#39; },
142+
&#39;method_2&#39; : x =&gt; x + &#39; ist gut&#39;
143+
}
144+
</code></pre></div>
145+
146+
<p>Wenn JSON als Austauschformat zwischen Sprachen verwendet wird darf es keine Funktionen enthalten.</p>
147+
148+
<div class="example">
149+
<pre><code class="language-php linenums"> $json_string = &quot;{
150+
&#39;prop1&#39; : &#39;Schokolade&#39;,
151+
&#39;prop2&#39; : 42,
152+
&#39;method_1&#39; : function (x) { return x + &#39; ist schlecht&#39; },
153+
&#39;method_2&#39; : x =&gt; x + &#39; ist gut&#39;
154+
}&quot;;
155+
$data = json_decode($json_string);
156+
if( $data == null ) {
157+
echo json_last_error_msg(); // Output: &quot;Syntax Error&quot;
158+
exit();
159+
}
160+
</code></pre></div>
161+
162+
<p>In PHP wird das gesamte JSON-Objekt verworfen, der Rückgabewert ist <code>null</code>
163+
und <code>json_last_error_msg()</code> gibt “Syntax Error” zurück.</p>
164+
90165
</div>
91166
<div class="slide">
92167

@@ -101,7 +176,7 @@ <h2 id="serialisierung">Serialisierung</h2>
101176
</div>
102177
<div class="slide">
103178

104-
<h2 id="objekt">Objekt</h2>
179+
<h3 id="objekt">Objekt</h3>
105180

106181
<p>Ein PHP Objekt lässt sich als JSON Objekt darstellen:</p>
107182

@@ -116,7 +191,7 @@ <h2 id="objekt">Objekt</h2>
116191
</div>
117192
<div class="slide">
118193

119-
<h2 id="array">Array</h2>
194+
<h3 id="array">Array</h3>
120195

121196
<p>Ein “normales” Array mit Integer als Index lässt sich
122197
direkt auf JSON abbilden:</p>
@@ -130,7 +205,7 @@ <h2 id="array">Array</h2>
130205
</div>
131206
<div class="slide">
132207

133-
<h2 id="assoziatives-array">Assoziatives Array</h2>
208+
<h3 id="assoziatives-array">Assoziatives Array</h3>
134209

135210
<p>Jedes Array kann in PHP auch Strings als Index enthalten.
136211
Ist das der Fall, dann wird es in JSON als Objekt dargestellt:</p>

0 commit comments

Comments
 (0)