From 66e009efde95af958e32bcf45b6a923c7a98d5ff Mon Sep 17 00:00:00 2001 From: justme1968 Date: Thu, 13 Feb 2014 14:35:50 +0000 Subject: [PATCH] fix for invalid json git-svn-id: https://svn.fhem.de/fhem/trunk@4908 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/32_withings.pm | 42 +++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/fhem/FHEM/32_withings.pm b/fhem/FHEM/32_withings.pm index 4b3f2365f..ab47c1401 100644 --- a/fhem/FHEM/32_withings.pm +++ b/fhem/FHEM/32_withings.pm @@ -6,7 +6,7 @@ package main; use strict; use warnings; -use Encode qw(encode_utf8); +use Encode qw(encode); use JSON; use LWP::Simple; use HTTP::Request; @@ -170,7 +170,9 @@ withings_getToken($) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); + my $once = $json->{body}{once}; $hash->{Token} = $once; @@ -209,7 +211,9 @@ withings_getSessionKey($) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); + foreach my $account (@{$json->{body}{account}}) { next if( !defined($account->{id}) ); $hash->{AccountID} = $account->{id} if($account->{email} eq $hash->{Login}); @@ -356,7 +360,8 @@ withings_getUsers($) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); my @users = (); foreach my $user (@{$json->{body}{users}}) { @@ -380,7 +385,8 @@ withings_getDevices($) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ );; my @devices = (); foreach my $association (@{$json->{body}{associations}}) { @@ -406,7 +412,8 @@ withings_getDeviceDetail($$) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); return $json->{body}; } @@ -431,19 +438,8 @@ withings_getDeviceReadings($$) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); - - if(open(FH, ") { - chomp $line; - next if($line =~ m/^#.*$/); - $content .= $line; - } - close(FH); - - $json = JSON->new->utf8(0)->decode($content); - } + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); return $json; } @@ -460,7 +456,8 @@ withings_getUserDetail($$) my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); - my $json = JSON->new->utf8(0)->decode($response->content); + my $json = (); + $json = JSON->new->utf8(0)->decode($response->content) if( $response->content =~ m/^{.*}$/ ); return $json->{body}{users}[0]; } @@ -556,7 +553,9 @@ withings_pollUser($) $url .= "&userid=$hash->{User}&publickey=$hash->{Key}"; $url .= "&lastupdate=$lastupdate" if( $lastupdate ); my $ret = get($url); - my $json = JSON->new->utf8(0)->decode($ret); + #my $json = JSON->new->utf8(0)->decode($ret); + my $json = (); + $json = JSON->new->utf8->decode(encode('UTF-8', $ret)) if( $ret =~ m/^{.*}$/ ); $hash->{status} = $json->{status}; if( $hash->{status} == 0 ) { @@ -646,7 +645,6 @@ withings_Get($$@) } } -Log 3, "$name: >$hash->{SUBTYPE}< >>$cmd<<"; return "Unknown argument $cmd, choose one of $list"; }