in reply to JSON ARRAY Problem
G'day Gruaig,
Welcome to the monastery.
Your description of what you're trying to extract doesn't make sense. You say what you want "has the date and sports name in it". You then show two elements: the first has "name":"World Cup 2014" (which is possibly related to some sport and has a year); the second has "name":"September 10th 2013" (which clearly has a date but makes no reference to any sport).
The code you've posted is not really of much help. It's incomplete and, as such, can't be run to reproduce what you're running; nor do we know what else it is supposed to be doing (we can only guess at the code completing the if statement and for loop). Furthermore, including line numbers (as part of the code) is also unhelpful: we'd need to remove them to run the code.
This code shows how to access the meta-tags elements and conditionally extract them:
#!/usr/bin/env perl use strict; use warnings; use JSON; use Data::Dumper; my $data = decode_json '{"id":241995,"name":"Georgia vs Finland","star +t":"2013-09-10T17:00:00.000Z","status":"paid","sport-id":15,"category +-id":[146598],"markets":[],"meta-tags":[{"id":4,"name":"Soccer","type +":"SPORT"},{"id":650,"name":"Qualifiers","type":"COMPETITION"},{"id": +5,"name":"World Cup 2014","type":"COMPETITION"},{"id":1,"name":"Sport +","type":"Root"},{"id":651,"name":"September 10th 2013","type":"DATE" +}],"in-running-flag":false,"allow-live-betting":false}'; my @extract; meta_wanted($_) && push @extract, $_ for @{$data->{'meta-tags'}}; print Dumper \@extract; sub meta_wanted { $_[0]->{type} eq 'SPORT' || $_[0]->{type} eq 'DATE' }
Output:
$VAR1 = [ { 'type' => 'SPORT', 'id' => 4, 'name' => 'Soccer' }, { 'name' => 'September 10th 2013', 'id' => 651, 'type' => 'DATE' } ];
You'll need to define meta_wanted() how you want it: what I have there is just a guess and likely to be not what you're after.
If you need to ask a follow-up question, please read the guidelines in "How do I post a question effectively?" before doing so.
-- Ken
|
|---|