shell本身不能存储复杂的数据结构,但是像shell中的大部分时间一样,你可以使用外部工具,我在这里演示了6种不同的解决方案,所有这些都在Unix *中像shell一样:

首先,您的JSON已损坏,这是file.js中的有效版本:

{

"italian" : {

"name" : [

"gatto",

"cane",

"pasta",

"telefono",

"libro"

],

"adjective" : [

"pesante",

"sottile",

"giallo",

"stretto"

]

},

"english" : {

"name" : [

"fish",

"book",

"guitar",

"piano"

],

"adjective" : [

"dirty",

"good",

"ugly",

"great"

]

}

}

使用jq

$jq '.english.adjective[1]' file.js

输出:

good

使用jq和RANDOM shell变量:

$echo $(

jq ".english.adjective[$((RANDOM%4))], .english.name[$((RANDOM%4))]" file.js

)

"great" "piano"

$rhino</dev/null

hash = $(

print(hash.english.adjective[1])

EOF

输出:

...

good

$node<

hash = $(

console.log(hash.english.adjective[1])

EOF

输出:

good

让我们在perl命令行中解析DS:

$perl -MJSON -0lnE '

$words = decode_json $_;

say $words->{english}->{adjective}->[1]

' file.js

输出:

good

$python<

import json

json_data = open('file.js')

data = json.load(json_data)

json_data.close()

print(data['english']['adjective'][1])

EOF

输出:

good

$ruby<

require 'json'

file = File.read('file.js')

data = JSON.parse(file)

print(data['english']['adjective'][1])

EOF

输出:

good

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐