以这些格式从JSON中提取数据
我一直在与JSON搞砸了一段时间,只是将其推出为文字,但并没有伤害任何人(我知道),但我想开始正确地做事。
这是我的代码:
term=temp['specifications']['PKG&HAZMAT']
for j in term:
try:
got=j['value']
except:
pass
print(got)
这是我的json文件:
"specifications": {
"PKG&HAZMAT": [{
"value": "FLETP",
"name": "VMRS",
"key": "a8f1W000000fxho"
},
{
"value": "EA",
"name": "Sales Unit",
"key": "a8f1W000000fxhv"
},
{
"value": "0",
"name": "Quantity per Application",
"key": "a8f1W000000fxhy"
},
{
"value": "5.8",
"name": "Height Each",
"key": "a8f1W000000fxi2"
},
{
"value": "20.35",
"name": "Width Each",
"key": "a8f1W000000fxi3"
},
{
"value": "18.95",
"name": "Length Each",
"key": "a8f1W000000fxi4"
},
{
"value": "14.47",
"name": "Weight Each",
"key": "a8f1W000000fxi5"
},
{
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
"name": "Prop 65 Statement",
"key": "a8f1W000000g3EN"
}
],
"MARKETING": [{
"value": "Spiral wound",
"name": "Benefit 1",
"key": "a8f1W000000TOAF"
},
{
"value": "Includes hang collar",
"name": "Benefit 2",
"key": "a8f1W000000TOAG"
},
{
"value": "One bundle for easy management",
"name": "Benefit 3",
"key": "a8f1W000000TOAH"
}
],
"PROP65": [{
"value": "1GENERAL",
"name": "Code",
"key": "a8f6S000000btYS"
},
{
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
"name": "Short Warning",
"key": "a8f6S000000btYT"
}
],
"FP_PartType_F552": [{
"value": "15",
"name": "Length",
"key": "a8f6S000000Ynnr"
},
{
"value": "ABS with zinc die cast plugs",
"name": "Electric Cable Type",
"key": "a8f6S000000YnYr"
}
]
},
我的输出是:
FLETP
EA
0
5.8
20.35
18.95
14.47
我希望像在这些格式中一样输出,如您在json文件中看到的,我想提取name
和value
从json文件中友好地做的错误,请让我知道
预期输出:
I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.
this is my code :
term=temp['specifications']['PKG&HAZMAT']
for j in term:
try:
got=j['value']
except:
pass
print(got)
this is my json file:
"specifications": {
"PKG&HAZMAT": [{
"value": "FLETP",
"name": "VMRS",
"key": "a8f1W000000fxho"
},
{
"value": "EA",
"name": "Sales Unit",
"key": "a8f1W000000fxhv"
},
{
"value": "0",
"name": "Quantity per Application",
"key": "a8f1W000000fxhy"
},
{
"value": "5.8",
"name": "Height Each",
"key": "a8f1W000000fxi2"
},
{
"value": "20.35",
"name": "Width Each",
"key": "a8f1W000000fxi3"
},
{
"value": "18.95",
"name": "Length Each",
"key": "a8f1W000000fxi4"
},
{
"value": "14.47",
"name": "Weight Each",
"key": "a8f1W000000fxi5"
},
{
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
"name": "Prop 65 Statement",
"key": "a8f1W000000g3EN"
}
],
"MARKETING": [{
"value": "Spiral wound",
"name": "Benefit 1",
"key": "a8f1W000000TOAF"
},
{
"value": "Includes hang collar",
"name": "Benefit 2",
"key": "a8f1W000000TOAG"
},
{
"value": "One bundle for easy management",
"name": "Benefit 3",
"key": "a8f1W000000TOAH"
}
],
"PROP65": [{
"value": "1GENERAL",
"name": "Code",
"key": "a8f6S000000btYS"
},
{
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
"name": "Short Warning",
"key": "a8f6S000000btYT"
}
],
"FP_PartType_F552": [{
"value": "15",
"name": "Length",
"key": "a8f6S000000Ynnr"
},
{
"value": "ABS with zinc die cast plugs",
"name": "Electric Cable Type",
"key": "a8f6S000000YnYr"
}
]
},
my output is these:
FLETP
EA
0
5.8
20.35
18.95
14.47
I want output like in these format as you have shown in the pic if you see in the JSON file I would like to extract name
and value
from the JSON file kindly what I can doing mistake please let me know
Expected Output:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做到这一点的两种方法:
pandas
将其放入dataframe/table(带有.json_normalize()
给定数据:< /strong>
代码1:
输出:
代码2:
输出:
Two ways to do that:
pandas
to put that into a dataframe/table (with.json_normalize()
Given data:
Code 1:
Output:
Code 2:
Output: