Aide et conseils concernant AutoIt et ses outils.
Utilisateur 3309 supprimé
Status :
Hors ligne
#1
Message
par Utilisateur 3309 supprimé » ven. 27 janv. 2017 18:23
Bonjour,
j'utilise le json.au3 de Ward (jsmn)
a partir d'un exemple tiré de celui-ci:
https://www.autoitscript.com/forum/topi ... nt=1079722
#include <Json.au3> local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]} ' Local $objJson = Json_Decode
( $Json1 ) Local $List = Json_ObjGet
( $objJson , "list" ) ; "list" is an array Local $objJson2 = $List [ 0 ] msgbox ( 0 , "" , $objJson2 )
Pourquoi $List[0] n'est pas = à "dt" ??
et donc comment faire pour obtenir l'array qui contient ceci: [dt, temp, pressure, humidity,.....],
Merci pour votre aide.
Modifié en dernier par Utilisateur 3309 supprimé le ven. 27 janv. 2017 18:40, modifié 1 fois.
Utilisateur 3309 supprimé
Status :
Hors ligne
#2
Message
par Utilisateur 3309 supprimé » ven. 27 janv. 2017 18:40
Réponse à moi même, il faut utiliser
Json_ObjGetKeys :
#include <Json.au3> #include <Array.au3> local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]} ' Local $objJson = Json_Decode
( $Json1 ) Local $List = Json_ObjGet
( $objJson , "list" ) ; "list" is an array Local $objJson2 = Json_ObjGetKeys
( $List [ 0 ] ) _ArrayDisplay ( $objJson2 )
Utilisateur 3309 supprimé
Status :
Hors ligne
#3
Message
par Utilisateur 3309 supprimé » ven. 27 janv. 2017 19:05
J'ai quand même une question...
il n'y a pas moyen d'éviter de faire X
Json_ObjGet pour aller chercher des clés qui sont au niveau X.
ici je dois en faire 2, pour récupérer "lon" et "lat"...
#include <Json.au3> #include <Array.au3> local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]} ' Local $objJson = Json_Decode
( $Json1 ) Local $List = Json_ObjGet
( $objJson , "city" ) Local $objJson1 = Json_ObjGet
( $List , "coord" ) Local $objJson2 = Json_ObjGetKeys
( $objJson1 ) _ArrayDisplay ( $objJson2 )
walkson
Modérateur
Messages : 1037 Enregistré le : ven. 12 août 2011 19:49
Localisation : Hurepoix
Status :
Hors ligne
#4
Message
par walkson » ven. 27 janv. 2017 22:04
Peut être comme ceci ?
#include <Json.au3> #include <Array.au3> ;https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/?do=findComment&comment=1291953 local $Json1 = '{"cod":"200","message":0.0268,"city":{"id":2950159,"name":"Berlin","coord":{"lon":13.41053,"lat":52.524368},"country":"DE","population":1000000},"cnt":1,"list":[{"dt":1368874800,"temp":{"day":13.22,"min":10.57,"max":13.22,"night":10.57,"eve":12.49,"morn":13.22},"pressure":1015.76,"humidity":100,"weather":[{"id":501,"main":"Rain","description":"mäßiger Regen","icon":"10"}],"speed":7.08,"deg":269,"clouds":92,"rain":7.75}]} ' $aReturned = JsonArrayfied
( $Json1 ) _ArrayDisplay ( $aReturned , "$aReturned" ) Func JsonArrayfied
( $sJsonString , $iEcho = 0 ) Local $sConsoleWriteJson = ConsoleWriteJson
( $sJsonString , "" , $iEcho ) Local $n , $aLines = StringSplit ( $sConsoleWriteJson , @LF , 1 ) Local $aTemp , $iRow = 0 , $iCol = 0 , $m , $aJsonArrayfied [ UBound ( $aLines ) + 1 ] [ 100 ] ; a lazy but efficient way to go about it For $n = 1 To $aLines [ 0 ] If StringInStr ( $aLines [ $n ] , ":" ) + 2 > StringLen ( $aLines [ $n ] ) Then ContinueLoop $aLines [ $n ] = StringReplace ( $aLines [ $n ] , "][" , "|" ) $aLines [ $n ] = StringReplace ( $aLines [ $n ] , "]" , "|" ) $aLines [ $n ] = StringReplace ( $aLines [ $n ] , "[" , "|" ) $aTemp = StringSplit ( $aLines [ $n ] , "|" ) $iRow += 1 For $m = 1 To $aTemp [ 0 ] - 1 If $iCol < $m Then $iCol = $m $aJsonArrayfied [ $iRow ] [ $m - 1 ] = StringReplace ( $aTemp [ $m ] , '"' , '' ) Next $aJsonArrayfied [ $iRow ] [ $aTemp [ 0 ] - 1 ] = StringTrimLeft ( $aTemp [ $aTemp [ 0 ] ] , StringInStr ( $aTemp [ $aTemp [ 0 ] ] , ":" ) + 1 ) $aJsonArrayfied [ $iRow ] [ 0 ] = StringMid ( $aTemp [ $aTemp [ 0 ] ] , 5 , StringInStr ( $aTemp [ $aTemp [ 0 ] ] , ":" ) - 5 ) Next $aJsonArrayfied [ 0 ] [ 0 ] = $iRow $aJsonArrayfied [ 0 ] [ 1 ] = $iCol ReDim $aJsonArrayfied [ $iRow + 1 ] [ $iCol + 1 ] Return $aJsonArrayfied EndFunc ;==>JsonArrayfied ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; added by me Func ConsoleWriteJson
( $sJsonString , $sDesc = "" , $iEcho = 1 ) Local $sOutGlobal If $sDesc = "" Then $sDesc = 'ConsoleWriteJson' Local $obj = Json_Decode
( $sJsonString ) Json_Iterate
( $sOutGlobal , $obj , '' , $sDesc , $iEcho ) Return $sOutGlobal EndFunc ;==>ConsoleWriteJson Func Json_Iterate
( ByRef $sOutGlobal , $obj , $string , $pre = "" , $iEcho = 1 ) Local $sOut = "" Local $temp , $i , $b If ( $pre <> "" ) Then $sOut &= $pre & ": " If $iEcho Then ConsoleWrite ( $pre & ": " ) EndIf $a = Json_Get_ShowResult
( $obj , $string , $sOutGlobal , $iEcho ) If IsArray ( $a ) Then For $i = 0 To UBound ( $a ) - 1 Json_Iterate
( $sOutGlobal , $obj , $string & '[' & $i & ']' , $pre , $iEcho ) Next ElseIf IsObj ( $a ) Then $b = Json_ObjGetKeys
( $a ) For $temp In $b Json_Iterate
( $sOutGlobal , $obj , $string & '["' & $temp & '"]' , $pre , $iEcho ) Next EndIf Return $sOutGlobal EndFunc ;==>Json_Iterate Func Json_Get_ShowResult
( $Var , $Key , ByRef $sOutGlobal , $iEcho ) Local $sOut = "" Local $Ret = Json_Getr
( $Var , $Key ) If @error Then Switch @error Case 1 $sOut &= "Error 1: key not exists" & @LF If $iEcho Then ConsoleWrite ( $sOut ) Case 2 $sOut &= "Error 2: syntax error" & @LF If $iEcho Then ConsoleWrite ( $sOut ) EndSwitch Else $sOut &= $Key & " => " & VarGetType ( $Ret ) & ": " & $Ret & @LF If $iEcho Then ConsoleWrite ( $sOut ) EndIf $sOutGlobal &= $sOut ;& $Ret Return $Ret EndFunc ;==>Json_Get_ShowResult Func Json_Getr
( $Var , $Key ) If Not $Key Then Return $Var Local $Match = StringRegExp ( $Key , "(^\[([^\]]+)\])" , 3 ) If IsArray ( $Match ) Then Local $Index = Json_Decode
( $Match [ 1 ] ) $Key = StringTrimLeft ( $Key , StringLen ( $Match [ 0 ] ) ) If IsString ( $Index ) And Json_IsObject
( $Var ) And Json_ObjExists
( $Var , $Index ) Then Local $Ret = Json_Getr
( Json_ObjGet
( $Var , $Index ) , $Key ) Return SetError ( @error , 0 , $Ret ) ElseIf IsNumber ( $Index ) And IsArray ( $Var ) And $Index >= 0 And $Index < UBound ( $Var ) Then Local $Ret = Json_Getr
( $Var [ $Index ] , $Key ) Return SetError ( @error , 0 , $Ret ) Else Return SetError ( 1 , 0 , "" ) EndIf EndIf Return SetError ( 2 , 0 , "" ) EndFunc ;==>Json_Getr
Pour l'include, pour ceux qui sont intéressés:
https://www.autoitscript.com/forum/topi ... nt=1286205
Cordialement,
Walkson
"Horas non numero nisi serenas " Le canon de midi
(Je ne compte que les heures heureuses)
Utilisateur 3309 supprimé
Status :
Hors ligne
#5
Message
par Utilisateur 3309 supprimé » sam. 28 janv. 2017 14:54
Merci beaucoup !