But you remember when we were learning HTML and we had to
escape our HTML content so it renders appropriately in the browser? If
not, we covered that in unit two I believe. JSON has, has
a similar issue. So, you know, if we have this little JSON
blob, you have a little dictionary that takes the key story and
maps it to the string once upon a time. You know, this
is valid JSON. Our key is surrounded by double quotes and our
value is surrounded by double quotes. What if we want to include double
quotes in our value? Now if I were to just put a
double quote in here, this would cause an invalid JSON because this
quote actually ends the string, and then we've got a bunch of
garbage after the string. You know, we'd need a comma and another opening
curly brace. You know, all sorts of things. That totally screwed things
up. Let's see what would happen if we tried to use that in
our terminal real quick. So I'm going to take that JSON string
that we just loaded. So this actually works. I'm using our Python string
is actually this whole piece. This is the, this is the
JSON string we're sending in to Python, and it's surrounded by single
quotes. Remember in Python you can use either single quotes or
double quotes to delineate a string, and that's what we're going to
use here. We're going to use single quotes so we can
use double quotes inside the JSON, and that works just fine. And
if I were to do what I just did in the editor,
and replace this p with a quote, let's see what happens. Explosion.
You know, the, the, the, JSON parser didn't like that at
all. The way we get around that is we escape this
quote by putting a slash in front of it. This escapes
the quote in Python, but what this turns into is still basically
the same string we had before, which is just the quote.
I actually have to escape both the slash and the quote
for this to work in Python. So, basically this slash is
the Python escape for this slash, which basically says we are inserting
a slash in the string, and yes, we mean to do that. The
JSON interpreter will see that slash. And say, okay, they must mean to include
this quote. And let's give this a run. There we go. The other way
to do this in Python that's a little simpler, instead of using double slashes,
which is kind of confusing, is, we could put an r in front
of our string, which says, this is a raw string, which basically means, Python,
ignore any escaping we're doing in here for the purposes of Python, and let
the json module interpret this slash however it will. And if we run this,
it also works, with our quote in the value. The answer as
we just saw, if want to include a quote in our value,
is to input a slash in front of it. And that's the
u that we had there before. So we have to escape our quotes
with a slash. This is not a thing if you want to read
in JSON, because we assume that the JSON we're going to be reading
is valid, and if its not valid, our JSON module will tell
us, it, it will throw an exception when we try to read it.
しかしHTMLではHTMLコンテンツを
エスケープしなければなりませんでした
ブラウザでレンダリングされるでしょうか?
それについてはレッスン2で話しました
JSONにも同様の問題があります JSONのBLOBがあって
storyというキーを取る辞書で
それを文字列にマップします これは有効なJSONです
キーと値はそれぞれダブルコーテーションで囲まれます
値にダブルコーテーションを含むにはどうするでしょう
ダブルコーテーションを入力しただけでは
これは無効です この引用符が文字列の最後にあって
そのあとに不要なデータがあるからです
必要なのはカンマと
波括弧です 不要なものがあるとすべて台無しです
私たちのターミナルでこれを使ったらどうなるか
見てみましょう 今読み込んだJSON文字列を使います
これは正しく機能します Python文字列を使いますが
この部分はJSON文字列です
Pythonに送るものでシングルコーテーションで囲みます
Pythonでは文字列を表すのにシングルコーテーションも
ダブルコーテーションも使えましたね
ですからここではシングルコーテーションを使い
JSONではダブルコーテーションを使えば問題ありません
私がエディタでやったように
このpをダブルコーテーションに変えると
すべてダメになります
JSONのパーサには嫌われました
ここから逃れるにはこのダブルコーテーションの前に
バックスラッシュを入れます Pythonでこの記号を
エスケープするためですが結果としては
元の文字列と同じものです
これをPythonで動作させるには
バックスラッシュとダブルコーテーション両方を
エスケープしなければなりません
このバックスラッシュは
Pythonがエスケープするもので
文字列にバックスラッシュを入れたのはそのためです
JSONインタプリタはバックスラッシュを問題にせず
ダブルコーテーションも含みます
これを実行すると結果はこうなります
これをPythonで行う別の方法はもう少し単純で
バックスラッシュを2つ使います
少し紛らわしいですが文字列の前にrを起き
rはraw文字列を表していてPythonは
Pythonのためにしているエスケープを無視し
JSONモジュールはバックスラッシュを読み取ります
これを実行すると
値にダブルコーテーションを含んで動作します
つまり答えは
もし値にダブルコーテーションがあったら
その直前にバックスラッシュを
そして以前あったuを入れます
つまりダブルコーテーションをエスケープするために
バックスラッシュを使いましたが
JSONでは読み込めません
私たちが読み込むJSONが有効だからです
もし有効でなければJSONモジュールは
読み込むと例外を表示します