总初学者需要帮助解析复杂 JSON 数组
Total Beginner Needs Help Parsing Complex JSON Array
* 使用新的、完整的 JSON 数据进行编辑 *
我要提前道歉,因为我是一个完全的编码新手。决定尝试一个项目的经理。
我希望有人可以帮助我提供一段代码来解析以下难看的 JSON 结果。它是 API 调用的结果,包含 4 "records"。由于格式问题,我不得不将其作为片段粘贴,抱歉。
{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","data":[{"group":[{"trailName":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Fair"}],"reportDate":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","statusCode":200,"timestamp":1484190106983},"timestamp":1484190107253,"sequenceNumber":0}}
{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","resourceId":"660750825d7a7e665acfd3a94ac3d20e","data":[{"group":[{"trailName":[{"text":"Greater Grayling Snowmobile Assoc. Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 09th, 2017 @ 10:50am"}]}]}]},"pageData":{"resourceId":"660750825d7a7e665acfd3a94ac3d20e","statusCode":200,"timestamp":1484190108241},"timestamp":1484190108467,"sequenceNumber":1}}
{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","resourceId":"63ba9e57962c0103cf401021656d5231","data":[{"group":[{"trailName":[{"text":"St. Helen SnowPackers Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Poor"}],"reportDate":[{"text":"January 09th, 2017 @ 5:02pm"}]}]}]},"pageData":{"resourceId":"63ba9e57962c0103cf401021656d5231","statusCode":200,"timestamp":1484190108869},"timestamp":1484190109341,"sequenceNumber":2}}
{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","resourceId":"d977860e12e8d285d5e7ea21e17bf43e","data":[{"group":[{"trailName":[{"text":"Cadillac Winter Promotions Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 10th, 2017 @ 3:25pm"}]}]}]},"pageData":{"resourceId":"d977860e12e8d285d5e7ea21e17bf43e","statusCode":200,"timestamp":1484190110156},"timestamp":1484190110365,"sequenceNumber":3}}
我正在尝试从每条记录中提取 trailName、trailCondition 和 reportDate 值,以便我可以将它们写在网页中(我已经记下了那部分)。但是由于 "values" 在树的较低节点中,并且有多个记录我不知所措。
JSON是由import.io创建的,我根本没有能力修改它。
我正在尝试 javascript 但我对所有选项持开放态度...适合编码新手。
感谢您的宝贵时间和怜悯... Marc
这项工作并不简单,因为格式看起来很精细,并且使用对象和数组来处理可能 key:value 对的内容。通用解决方案将需要一些努力。
以下是如何显式获取您似乎想要的一些数据,这些数据位于 group 数组中。也许你可以根据自己的需要进行调整。
var data = JSON.parse('{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"bb56c59af972a87e62bcba4206a05d4d","data":[{"group":[{"Trail Name":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"Trail Condition":[{"text":"Fair"}],"Report Date":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"bb56c59af972a87e62bcba4206a05d4d","statusCode":200,"timestamp":1484143736420},"timestamp":1484143957298,"sequenceNumber":0}}');
// Get the group, which is an array
var group = data.result.extractorData.data[0].group;
// Get data from the group
group.forEach(function(group) {
Object.keys(group).forEach(function(key) {
console.log(key + ': ' + group[key][0].text);
});
});
根据编辑好的OP,下面将处理多条记录。要注意的部分是 data 是 group 对象的数组。您的示例只有一组,因此代码只是 data[0].group
.
该格式允许每个 data 数组中有多个 group 对象,因此将来可能会有聪明人利用它(或也许不是,但很容易防范。
所以您可能应该遍历 data 数组并提取每个组,即使目前只有一个。这意味着您还需要处理返回结果对象数组,而不仅仅是单个对象。
// Assuming data arrives as records separated by returns
var data = '{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","data":[{"group":[{"trailName":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Fair"}],"reportDate":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","statusCode":200,"timestamp":1484190106983},"timestamp":1484190107253,"sequenceNumber":0}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","resourceId":"660750825d7a7e665acfd3a94ac3d20e","data":[{"group":[{"trailName":[{"text":"Greater Grayling Snowmobile Assoc. Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 09th, 2017 @ 10:50am"}]}]}]},"pageData":{"resourceId":"660750825d7a7e665acfd3a94ac3d20e","statusCode":200,"timestamp":1484190108241},"timestamp":1484190108467,"sequenceNumber":1}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","resourceId":"63ba9e57962c0103cf401021656d5231","data":[{"group":[{"trailName":[{"text":"St. Helen SnowPackers Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Poor"}],"reportDate":[{"text":"January 09th, 2017 @ 5:02pm"}]}]}]},"pageData":{"resourceId":"63ba9e57962c0103cf401021656d5231","statusCode":200,"timestamp":1484190108869},"timestamp":1484190109341,"sequenceNumber":2}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","resourceId":"d977860e12e8d285d5e7ea21e17bf43e","data":[{"group":[{"trailName":[{"text":"Cadillac Winter Promotions Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 10th, 2017 @ 3:25pm"}]}]}]},"pageData":{"resourceId":"d977860e12e8d285d5e7ea21e17bf43e","statusCode":200,"timestamp":1484190110156},"timestamp":1484190110365,"sequenceNumber":3}}';
// Function to process each record and return an object like:
// {trailName: value,
// trailCondition: value,
// reportDate: value}
function processRecord(record) {
record = JSON.parse(record);
var group = record.result.extractorData.data[0].group;
var result = {}
group.forEach(function(group) {
Object.keys(group).forEach(function(key) {
result[key] = group[key][0].text;
});
});
return result;
}
// Split the data into records on new lines,
// Process each record and return result objects in an array
var processedData = data.split('\n').map(record => processRecord(record));
// Display results: for each record, write out each key and its value
processedData.forEach(function (record) {
Object.keys(record).forEach(function(key) {
console.log(key + ': ' + record[key]);
})
});
// Which can also be written using arrow functions, but it's a bit obfuscated:
/*
processedData.forEach(record =>
Object.keys(record).forEach(key =>
console.log(key + ': ' + record[key])
)
);
*/
您也可以将结果格式化为 table,但我会把它留给您。 ;-)
* 使用新的、完整的 JSON 数据进行编辑 *
我要提前道歉,因为我是一个完全的编码新手。决定尝试一个项目的经理。
我希望有人可以帮助我提供一段代码来解析以下难看的 JSON 结果。它是 API 调用的结果,包含 4 "records"。由于格式问题,我不得不将其作为片段粘贴,抱歉。
{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","data":[{"group":[{"trailName":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Fair"}],"reportDate":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","statusCode":200,"timestamp":1484190106983},"timestamp":1484190107253,"sequenceNumber":0}}
{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","resourceId":"660750825d7a7e665acfd3a94ac3d20e","data":[{"group":[{"trailName":[{"text":"Greater Grayling Snowmobile Assoc. Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 09th, 2017 @ 10:50am"}]}]}]},"pageData":{"resourceId":"660750825d7a7e665acfd3a94ac3d20e","statusCode":200,"timestamp":1484190108241},"timestamp":1484190108467,"sequenceNumber":1}}
{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","resourceId":"63ba9e57962c0103cf401021656d5231","data":[{"group":[{"trailName":[{"text":"St. Helen SnowPackers Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Poor"}],"reportDate":[{"text":"January 09th, 2017 @ 5:02pm"}]}]}]},"pageData":{"resourceId":"63ba9e57962c0103cf401021656d5231","statusCode":200,"timestamp":1484190108869},"timestamp":1484190109341,"sequenceNumber":2}}
{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","resourceId":"d977860e12e8d285d5e7ea21e17bf43e","data":[{"group":[{"trailName":[{"text":"Cadillac Winter Promotions Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 10th, 2017 @ 3:25pm"}]}]}]},"pageData":{"resourceId":"d977860e12e8d285d5e7ea21e17bf43e","statusCode":200,"timestamp":1484190110156},"timestamp":1484190110365,"sequenceNumber":3}}
我正在尝试从每条记录中提取 trailName、trailCondition 和 reportDate 值,以便我可以将它们写在网页中(我已经记下了那部分)。但是由于 "values" 在树的较低节点中,并且有多个记录我不知所措。
JSON是由import.io创建的,我根本没有能力修改它。
我正在尝试 javascript 但我对所有选项持开放态度...适合编码新手。
感谢您的宝贵时间和怜悯... Marc
这项工作并不简单,因为格式看起来很精细,并且使用对象和数组来处理可能 key:value 对的内容。通用解决方案将需要一些努力。
以下是如何显式获取您似乎想要的一些数据,这些数据位于 group 数组中。也许你可以根据自己的需要进行调整。
var data = JSON.parse('{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"bb56c59af972a87e62bcba4206a05d4d","data":[{"group":[{"Trail Name":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"Trail Condition":[{"text":"Fair"}],"Report Date":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"bb56c59af972a87e62bcba4206a05d4d","statusCode":200,"timestamp":1484143736420},"timestamp":1484143957298,"sequenceNumber":0}}');
// Get the group, which is an array
var group = data.result.extractorData.data[0].group;
// Get data from the group
group.forEach(function(group) {
Object.keys(group).forEach(function(key) {
console.log(key + ': ' + group[key][0].text);
});
});
根据编辑好的OP,下面将处理多条记录。要注意的部分是 data 是 group 对象的数组。您的示例只有一组,因此代码只是 data[0].group
.
该格式允许每个 data 数组中有多个 group 对象,因此将来可能会有聪明人利用它(或也许不是,但很容易防范。
所以您可能应该遍历 data 数组并提取每个组,即使目前只有一个。这意味着您还需要处理返回结果对象数组,而不仅仅是单个对象。
// Assuming data arrives as records separated by returns
var data = '{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/pere-marquette-snowmobile-club/","resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","data":[{"group":[{"trailName":[{"text":"Pere Marquette Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Fair"}],"reportDate":[{"text":"January 10th, 2017 @ 11:20am"}]}]}]},"pageData":{"resourceId":"29ce67ff3db8907d01f84ad45b6b47e4","statusCode":200,"timestamp":1484190106983},"timestamp":1484190107253,"sequenceNumber":0}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/greater-grayling-snowmobile-assoc/","resourceId":"660750825d7a7e665acfd3a94ac3d20e","data":[{"group":[{"trailName":[{"text":"Greater Grayling Snowmobile Assoc. Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 09th, 2017 @ 10:50am"}]}]}]},"pageData":{"resourceId":"660750825d7a7e665acfd3a94ac3d20e","statusCode":200,"timestamp":1484190108241},"timestamp":1484190108467,"sequenceNumber":1}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/st-helen-snowmobile-club/","resourceId":"63ba9e57962c0103cf401021656d5231","data":[{"group":[{"trailName":[{"text":"St. Helen SnowPackers Snowmobile Club Trail Report"}],"trailCondition":[{"text":"Poor"}],"reportDate":[{"text":"January 09th, 2017 @ 5:02pm"}]}]}]},"pageData":{"resourceId":"63ba9e57962c0103cf401021656d5231","statusCode":200,"timestamp":1484190108869},"timestamp":1484190109341,"sequenceNumber":2}}\n' +
'{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","result":{"extractorData":{"url":"http://www.msasnow.org/trail-reports/cadillac-winter-promotions/","resourceId":"d977860e12e8d285d5e7ea21e17bf43e","data":[{"group":[{"trailName":[{"text":"Cadillac Winter Promotions Trail Report"}],"trailCondition":[{"text":"Good"}],"reportDate":[{"text":"January 10th, 2017 @ 3:25pm"}]}]}]},"pageData":{"resourceId":"d977860e12e8d285d5e7ea21e17bf43e","statusCode":200,"timestamp":1484190110156},"timestamp":1484190110365,"sequenceNumber":3}}';
// Function to process each record and return an object like:
// {trailName: value,
// trailCondition: value,
// reportDate: value}
function processRecord(record) {
record = JSON.parse(record);
var group = record.result.extractorData.data[0].group;
var result = {}
group.forEach(function(group) {
Object.keys(group).forEach(function(key) {
result[key] = group[key][0].text;
});
});
return result;
}
// Split the data into records on new lines,
// Process each record and return result objects in an array
var processedData = data.split('\n').map(record => processRecord(record));
// Display results: for each record, write out each key and its value
processedData.forEach(function (record) {
Object.keys(record).forEach(function(key) {
console.log(key + ': ' + record[key]);
})
});
// Which can also be written using arrow functions, but it's a bit obfuscated:
/*
processedData.forEach(record =>
Object.keys(record).forEach(key =>
console.log(key + ': ' + record[key])
)
);
*/
您也可以将结果格式化为 table,但我会把它留给您。 ;-)