18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JSONをRuby,Python,JavaScript,PHPで扱う方法

Last updated at Posted at 2015-08-15

Python, Ruby, JavaScript,PHPで、JSONのエンコードとデコードの方法について確認してました。

Pythonの場合

test_json.py
#!/usr/bin/env python                                                           
# -*- coding:utf-8 -*-                                                          
#                                                                               
import json

# 辞書形式のデータ
dic1 = {"key1": None, "key2": None}
dic1["key1"] = 24
dic1["key2"] = 45
print dic1

# 辞書形式からJSONに変換
json_data1 = json.dumps(dic1, sort_keys=True,indent=4)
print json_data1

json_data2 = json.dumps(dic1,sort_keys=True)
print json_data2

json_data3 = json.dumps(dic1)
print json_data3

dic2 = json.loads(json_data1)
print dic2

# JSONから辞書形式に再変換
dic2 = json.loads(json_data2)
print dic2

dic2 = json.loads(json_data3)
print dic2

# 要素の再確認
print dic2["key1"]
print dic2["key2"]
実行結果
$ ./test_json.py
{'key2': 45, 'key1': 24}
{
    "key1": 24, 
    "key2": 45
}
{"key1": 24, "key2": 45}
{"key2": 45, "key1": 24}
{u'key2': 45, u'key1': 24}
{u'key2': 45, u'key1': 24}
{u'key2': 45, u'key1': 24}
24
45

Rubyの場合

test_json.rb
#!/usr/bin/env ruby                                                             
# -*- coding:utf-8 -*-                                                          
#                                                                               
require 'json'

# ハッシュ形式データ
h = {"key1" => 10, "key2" => 40, "key3" => "Window10"}
p h
p h["key1"]
p h["key2"]
p h["Windows10"]

# ハッシュからJSON形式へ変換
j = JSON.generate(h)
p j

# JSONからハッシュへ変換
x = JSON.parse(j)
p x
実行結果
$ ./test_json.rb
{"temp"=>10, "humid"=>40, "pos"=>"window"}
10
40
"window"
"{\"temp\":10,\"humid\":40,\"pos\":\"window\"}"
{"temp"=>10, "humid"=>40, "pos"=>"window"}

JavaScriptの場合

コマンドラインから実行できるスクリプトです。

test_json.js
#!/usr/bin/env nodejs
//
var sys = require('util');

// 連想配列
var d = { key1: 24 , key2: 45 , key3: "Windows10"}; 
sys.print(d['key1']+"\n")
sys.print(d['key2']+"\n")
sys.print(d['key3']+"\n")

// 連想配列からJSON形式へ
var json_text = JSON.stringify(d);
sys.print(json_text + "\n")

// JSON形式から連想配列のオブジェクトに戻す
var obj = JSON.parse(json_text)
sys.print(obj['key1']+"\n")
sys.print(obj['key2']+"\n")
sys.print(obj['key3']+"\n")
実行結果
$ ./test_json.js
24
45
Windows10
{"key1":24,"key2":45,"key3":"Windows10"}
24
45
Windows10

#PHP5の場合

test_json.php
#!/usr/bin/env php5
<?PHP

/* 連想配列 */
$arr = array('key1' => 24, 'key2' =>45, 'key3' => 'windows');
print "{$arr['key1']}\n";
print "{$arr['key2']}\n";
print "{$arr['key3']}\n";

/* JSON形式のテキストに変換 */
$json_text = json_encode($arr);
print "{$json_text}\n";

/* 連想配列に変換 */
$obj = json_decode($json_text);
print "{$obj->{'key1'}}\n";
print "{$obj->{'key2'}}\n";
print "{$obj->{'key3'}}\n";

?>
実行結果
$ ./test_json.php 
24
45
windows
{"key1":24,"key2":45,"key3":"windows"}
24
45
windows
18
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?