*使用新的,完整的JSON数据*
进行编辑我要事先道歉,因为我是一个完全编码的新手。决定尝试项目的经理。
我希望有人可以帮助我使用代码片段来解析以下丑陋的JSON结果。这是API调用的结果,包含4个“记录”。由于格式化,我不得不将其粘贴为片段,道歉。
{"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值,以便我可以在网页中将它们写出来(我已将该部分删除)。但由于“值”位于树的较低节点,并且有多个记录我不知所措。
JSON是由import.io创建的,我根本无法修改它。
我正在尝试使用javascript,但我对所有选项都持开放态度......面向编码新手。
感谢您宝贵的时间和怜悯...... Marc
答案 0 :(得分:1)
这项工作并不简单,因为格式似乎是精细的,并且使用对象和数组来处理可能是关键的事物:值对。一般解决方案需要一些努力。
以下是如何显式获取您似乎想要的一些数据,这些数据位于组数组中。也许你可以根据自己的需要调整它。
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 数组并提取每个组,即使当前只有一个。这意味着你还需要处理返回说一个结果对象的数组,而不仅仅是单个对象。
// 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])
)
);
*/
你也可以将结果格式化为表格,但我会把它留给你。 ; - )