問題 ccnp-350-401-topic-1-question-952・第3版

問題

PythonのオブジェクトをJSON文字列へ変換する。空欄[1]〜[3]へ入れる候補の順序はどれか。使用しない候補もある。

候補:① JSONEncoder、② data、③ decode、④ encode、⑤ JSONDecoder

import json

data = {
  "measurement": "cefcFRUPowerOperStatus",
  "maxDataPoints": 45,
  "alert": "True",
  "errorDescription": None,
  "devices": [{"model": "Cisco 4331 ISR"}, {"model": "Cisco 3500 S"}]
}

obj = json.[1]().[2]([3])

print(obj)

JSONEncoderのコードと候補

  1. A 

    ①→④→②

  2. B 

    ⑤→③→②

  3. C 

    ①→③→②

  4. D 

    ⑤→④→②

正解:A

理由

JSONEncoderを生成してencode(data)を呼び、変換したJSON文字列を表示する。alertは文字列のTrueで、errorDescriptionのNoneはJSONのnullになる。

選択肢ごとの説明

A.

①→④→②

JSONEncoder().encode(data)でPythonオブジェクトをJSONへ書き出す。

B.

⑤→③→②

JSONDecoder().decodeはJSON文字列の読込みである。

C.

①→③→②

JSONEncoderへdecodeを指定しても、このエンコーダーの書出しメソッドにならない。

D.

⑤→④→②

JSONDecoderは符号化用のJSONEncoderではなく、このencode呼出しに対応しない。

お問い合わせ

演習する →