2014-10-27 102 views
0

我想編寫一個程序,它可以創建JSON格式的輸出,怎麼會這樣做呢?和編程語言?創建JSON格式輸出

這是JSON(預期輸出)的示例輸出,我需要在腳本執行過程中以用戶友好的方式在Name,Gender,Qualification和其他屬性中輸入。並以下列JSON格式輸出。對不起,我是編程新手,但對學習Perl(或)Python(或)Java非常感興趣。這裏最好的是什麼?

有什麼建議嗎?

P.S對不起,我對JSON也很新,請爲我這個基本的道歉。

[ 
    { 
     "Name":"Steven Mark", 
     "gender":"male", 
     "Qualification": { 
      "college":"Bachelor in Science", 
      "tech":"certified pro" 
     }, 
     "contributions": [ 
      { 
       "name":"biography", 
       "type":"book", 
      }, 
     ] 
    }, 
    { 
     "Name":"Andrea Mark", 
     "Gender":"female", 
     "Qualifications": { 
      "college":"Bachelor in physics", 
     }, 
     "contributions": [ 
      { 
       "name":"my insights", 
       "type":"movie", 
      }, 
     ] 
    } 
] 
+0

您需要先決定至少要使用哪種語言;沒有這個決定(這是你的決定),這個問題太廣泛而無法回答。 – 2014-10-27 08:50:12

+0

這不是有效的JSON。學會正確縮進以發現錯誤 – ikegami 2014-10-27 10:30:40

回答

0

如果你同意使用任何編程語言,我可以建議python。憑藉其JSON lib中,你可以做以下兩行(以#爲註釋):

# import lib 
import json 
# fill data into variable (this is list with dict objects inside): 
data = [{"name":"john"},{"name": "bill"}] 
# dump json 
json.dumps(data) 

將輸出數據作爲JSON。 你可以,如果你要使用Python使用的東西從https://wiki.python.org/moin/BeginnersGuide

0

開始編寫Python,你可以嘗試使用simplejsonjson模塊來創建一個JSON對象。

例如,

try: 
    import simplejson 
except: 
    import json 

data = dict(a=1,b=2) 
with open("results.json", "w") as fp: 
    json.dump(data, fp, indent=3, encoding="utf-8") 

對於傾倒,JSON是(由一個數量級但不)超過simplejson更快。對於加載,simplejson更快(但不是一個數量級)。

您可以檢查heresimplejsonjson之間更詳細的比較。

2

幾乎每種語言都有一個JSON庫,包括Perl。

use JSON; 

my $data = [ 
    { 
     "Name" => "Steven Mark", 
     "gender" => "male", 
     "Qualification" => { 
      "college" => "Bachelor in Science", 
      "tech" => "certified pro" 
     }, 
     "contributions" => [ 
      { 
       "name" => "biography", 
       "type" => "book", 
      }, 
     ] 
    }, 
    { 
     "Name" => "Andrea Mark", 
     "Gender" => "female", 
     "Qualifications" => { 
      "college" => "Bachelor in physics", 
     }, 
     "contributions" => [ 
      { 
       "name" => "my insights", 
       "type" => "movie", 
      }, 
     ] 
    } 
]; 

print(encode_json($data));