Page 1 sur 1
[R] récupérer un array d'un json
Posté : ven. 27 janv. 2017 18:23
par Utilisateur 3309 supprimé
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 arrayLocal $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.
Re: [..] récupérer un array d'un json
Posté : ven. 27 janv. 2017 18:40
par Utilisateur 3309 supprimé
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 arrayLocal $objJson2 = Json_ObjGetKeys
($List[0])_ArrayDisplay($objJson2)
Re: [R] récupérer un array d'un json
Posté : ven. 27 janv. 2017 19:05
par Utilisateur 3309 supprimé
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)
Re: [R] récupérer un array d'un json
Posté : ven. 27 janv. 2017 22:04
par walkson
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 $aJsonArrayfiedEndFunc ;==>JsonArrayfied;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; added by meFunc ConsoleWriteJson
($sJsonString, $sDesc = "", $iEcho = 1) Local $sOutGlobal If $sDesc = "" Then $sDesc = 'ConsoleWriteJson' Local $obj = Json_Decode
($sJsonString) Json_Iterate
($sOutGlobal, $obj, '', $sDesc, $iEcho) Return $sOutGlobalEndFunc ;==>ConsoleWriteJsonFunc 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 $sOutGlobalEndFunc ;==>Json_IterateFunc 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 $RetEndFunc ;==>Json_Get_ShowResultFunc 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
Re: [R] récupérer un array d'un json
Posté : sam. 28 janv. 2017 14:54
par Utilisateur 3309 supprimé
Merci beaucoup !