Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^5: JSON module

by LTjake (Prior)
on Aug 15, 2007 at 12:06 UTC ( #632725=note: print w/ replies, xml ) Need Help??


in reply to Re^4: JSON module
in thread JSON module

Your hash is being flattened to a list in this line:

push @output, %hashData;

You really want a hash reference there:

push @output, \%hashData;

--
"Go up to the next female stranger you see and tell her that her "body is a wonderland."
My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
" (src)


Comment on Re^5: JSON module
Select or Download Code
Re^6: JSON module
by hallikpapa (Scribe) on Aug 15, 2007 at 21:49 UTC
    <edit> Looks like I need an array of hashes, because the records were getting overwritten. But doing something like this will screw up the JSON:
    $counter++; $hashData{$counter}{'id'} = $hold_col_val1; $hashData{$counter}{'col2'} = $hold_col_val2; $hashData{$counter}{'col3'} = $hold_col_val3; $hashData{$counter}{'col4'} = $total_cdr_count; $hashData{$counter}{'col5'} = $total_call_count; $hashData{$counter}{'col6'} = $total_error_count; push @output, \%hashData;
    I appreciate your help with all of this. I haven't touched perl in a while and trying to get the hash to work. I comes out like this now, and the extra digit at the beginning of every record. Can it be removed before encoding it in JSON?
    $sth->execute(); while (my $row = $sth->fetchrow_hashref ){ push @newRow, $row; $gt_cdrs = $gt_cdrs + $row->{"col4"}; $gt_hold_time=$gt_hold_time + $row->{"col5"}; if ($row->{"CALL_STATUS"} eq "S" || $row->{"CALL_STATUS"} eq " +R" || $row->{"CALL_STATUS"} eq "I") { $gt_duration = $gt_duration + $row->{"col6"}; } else { $gt_errs = $gt_errs + $row->{"col4"}; } $gt_calls = ($gt_cdrs - $gt_errs); ################################# if (($hold_col_val1 ne $row->{"id"}) || ($hold_col_val2 ne $row->{"co +l2"}) || ($hold_col_val3 ne $row->{"col3"})) { unless ($hold_col_val1 eq "~~~") { ## calculate percentages $ASR = (($total_cdr_count-$total_error_count) +/ $total_cdr_count) * 100; if (($total_cdr_count-$total_error_count) > 0) + { $avgPDD = $good_hold_time / ($total_cd +r_count - $total_error_count); $avgDUR = $good_duration / ($total_cdr +_count - $total_error_count); } else { $avgPDD = 0; $avgDUR = 0; } $minutes = $good_duration / 60; if($asr_scrn ne "") { if( ($asr1 ne "" ) && ($asr2 eq "") ) +{ if ($ASR > $asr1) { goto EXTCHK1; } } if( ($asr1 ne "" ) && ($asr2 ne "") ) +{ if ( ($ASR < $asr1 ) || ($ASR +> $asr2+1) ) { goto EXTCHK1; } } } $formatASR = sprintf("%3.0d",$ASR); $formatPDD = sprintf("%3.0d",$avgPDD); $formatDUR = sprintf("%3.0d",$avgDUR); $formatMin = sprintf("%4.1f",$minutes); } $counter++; $hashData{'counter'} = $counter; $hashData{$counter}{'id'} = $hold_col_val1; $hashData{$counter}{'col2'} = $hold_col_val2; $hashData{$counter}{'col3'} = $hold_col_val3; $hashData{$counter}{'col4'} = $total_cdr_count; $hashData{$counter}{'col5'} = $total_call_count; $hashData{$counter}{'col6'} = $total_error_count; push @output, \%hashData; #print $hashData[$counter]{'id'}." ".$hashData[$counte +r]{'col6'}."\n"; }

      @output should be an array of hashrefs.

      while ( my $row = $sth->fetchrow_hashref ) { my %data; $data{ col1 } = 'bar'; push @output, \%data; }

      Then converting it to JSON will make it look something like (i've added some formatting):

      { "myData" : [ { "col1" : "bar" }, { "col1" : "bar" }, # etc ... ] }

      HTH.

      --
      "Go up to the next female stranger you see and tell her that her "body is a wonderland."
      My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
      " (src)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://632725]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-07-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls