gpt4 book ai didi

json - 逐 block 解析 JSON

转载 作者:行者123 更新时间:2023-12-02 08:28:12 25 4
gpt4 key购买 nike

我有一个包含客户和日期列表的 JSON 文件。

文件看起来像这样:

{
"Customers": [
{
"Customer": "Customer Name Here",
"Company": "Super Coffee",
"First Name": "First Name Here",
"Main Phone": "777-777-7777",
"Fax": "777-777-7777",
"Bill to 1": "Billing Address One",
"Bill to 2": "Billing Address Two",
"Bill to 3": "Billing Address Three",
"Ship to 1": "Shipping Address One",
"Ship to 2": "Shipping Address Two",
"Ship to 3": "Shipping Address Three",
"Customer Type": "Dealer/Retail"
},
{
"Customer": "Customer Name Here",
"Company": "Turtle Mountain Welding",
"First Name": "First Name Here",
"Main Phone": "777-777-7777",
"Fax": "777-777-7777",
"Bill to 1": "Billing Address One",
"Bill to 2": "Billing Address Two",
"Bill to 3": "Billing Address Three",
"Ship to 1": "Shipping Address One",
"Ship to 2": "Shipping Address Two",
"Ship to 3": "Shipping Address Three",
"Customer Type": "Dealer/Retail"
},
{
"Customer": "Customer Name Here",
"Company": "Mountain Equipment Coop",
"First Name": "First Name Here",
"Main Phone": "777-777-7777",
"Fax": "777-777-7777",
"Bill to 1": "Billing Address One",
"Bill to 2": "Billing Address Two",
"Bill to 3": "Billing Address Three",
"Ship to 1": "Shipping Address One",
"Ship to 2": "Shipping Address Two",
"Ship to 3": "Shipping Address Three",
"Customer Type": "Dealer/Retail"
},
{
"Customer": "Customer Name Here",
"Company": "Best Soup Inc.",
"First Name": "First Name Here",
"Main Phone": "777-777-7777",
"Fax": "777-777-7777",
"Bill to 1": "Billing Address One",
"Bill to 2": "Billing Address Two",
"Bill to 3": "Billing Address Three",
"Ship to 1": "Shipping Address One",
"Ship to 2": "Shipping Address Two",
"Ship to 3": "Shipping Address Three",
"Customer Type": "Dealer/Retail"
}
]
}

我需要能够逐 block 而不是逐行地从文件中提取数据。

我习惯于逐行解析文件以获取数据,但是对于 JSON,我需要以某种方式逐 block 读取它(或者更准确地说,逐个对象?)。我需要为每个客户阅读括号内的内容。这样我就可以编写一个脚本来提取我需要的数据,并从中构建一个 CSV 文件。

例如:

i="1"
for file in *.json; do
customername=$(jsonblock$i:customername);
customerAddress=$(jsonblock$i:customeraddress);
etc...
i=$[i+1]
done

我明白在逐行读取文件时这是如何完成的,但是我如何才能读取每个 JSON block ,就好像它是一行一样?

最佳答案

对于上面的 JSON(由于提供的数据无效而进行了修改),以下脚本将解析并打印每个 block 的 "Company:" 部分:

#!/usr/bin/env perl

use JSON;
use IO::All;
use v5.16;

my $data < io 'Our_Customers.json';
my $customers_list = decode_json($data)->{"Customers"};

for my $customer (@$customers_list) {
say $customer->{"Company"} ;
}

输出:

Super Coffee
Turtle Mountain Welding
Mountain Equipment Coop
Best Soup Inc.

脚本使用IO::AllJSON读取和解析 (decode_json) 文件。

在这个例子中,JSON 数据被简单地映射到一个 Perl 数据结构(一个 Array of Hashes ),它与 JSON 数据完全对应。然后我们可以访问每个数组元素(数组中的每个散列),然后通过键名访问散列中的数据。 Perl 具有非常灵活的数据处理和访问功能,这使得处理 JSON 数据变得非常愉快。

每个数据 block 的键来自 JSON 文件的等效部分。如果我们将一个元素移出数组,它将是一个散列,我们访问可以看到元素的 keysvalues,如下所示:

say for keys shift $customers_list ;

Customer Type
First Name
Bill to 2
Main Phone
...

使用您在 for 循环中看到的 $element->{"key"} 语法访问每个键的值。


最好在将 JSON 数据发布到 SO 之前对其进行验证 - JSON Lint类似的服务可以帮助解决这个问题。

关于json - 逐 block 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com