我想使用以下JSON,它是我从AutoIt的API中读出的。
[
{
"id": 2336916545,
"item_id": 85016,
"price": 103,
"quantity": 250,
"created": "2020-06-07T18:13:46+00:00"
},
{
"id": 2336916458,
"item_id": 85016,
"price": 103,
"quantity": 250,
"created": "2020-06-07T18:13:34+00:00"
},
{
"id": 2336916352,
"item_id": 85016,
"price": 103,
"quantity": 250,
"created": "2020-06-07T18:13:22+00:00"
}
]
怎样用最简单的方法将 "价格 "和 "数量 "相乘,得到数组的总和?
谢谢您的帮助
你可以使用沃德的作品及其库。JSON.au3 来处理JSON文件。
要在你的AutoIt项目中轻松添加这个库,你可以使用AGS-wrapper-json包。https:/www.npmjs.compackage@autoit-gui-skeletonags-wrapper-json。. 一个AGS-wrapper是一个简单的包装器,用于将另一个团队开发者创建的另一个库集成到AGS-wrapper中。AGS框架. 更多关于 AGS中AutoIt的依赖管理器
安装它。
λ yarn add @autoit-gui-skeleton/ags-wrapper-json --modules-folder vendor
最后,要在你的AutoIt程序中使用这个库,你需要在主程序中包含这个库。不需要额外的配置来使用它。
#include './vendor/@autoit-gui-skeleton/ags-wrapper-json/JSON.au3'
根据Ward的文档。
最JSON数据类型将被解码成相应的AutoIt变量,包括1D数组、字符串、数字、true、false和null。JSON对象将被解码成由ObjCreate("Scripting.Dictionary")重新调出的 "Windows Scripting Dictionary对象"。AutoIt内置的IsArray、IsBool等函数可以用来检查返回的数据类型。但对于Object和Null,应该使用Json_IsObject()和Json_IsNull()。
如果输入的JSON字符串是无效的,@Error将被设置为$JSMN_ERROR_INVAL。如果输入的JSON字符串没有完成,@Error将被设置为$JSMN_ERROR_PART。
;====================================================================================
; Decode JSON from a given local file
;
; @param $jsonfilePath (string)
; @return $object (object), instance return by json_decode
;====================================================================================
Func json_decode_from_file($filePath)
Local $fileOpen, $fileContent, $object
$fileOpen = FileOpen($filePath, $FO_READ)
If $fileOpen = -1 Then
Return SetError(1, 0, "An error occurred when reading the file " & $filePath)
EndIf
$fileContent = FileRead($fileOpen)
FileClose($fileOpen)
$object = Json_Decode($fileContent)
Return $object
EndFunc
要使用json_decode_from_file返回的$jsonObject进行工作,可以使用Json_Get函数。要选择一个JSON变量,可以支持点符号和方括号符号。
例如,.JSON_Get函数可以选择一个对象集合,也可以选择一个方括号。
Local $file = @ScriptDir & "\assets\DROIDS.json"
Local $jsonObject = json_decode_from_file($file)
; With dot notation
Local $project = Json_Get($jsonObject, '.project') ; Listing droids
Local $name = Json_Get($jsonObject, '.author.name') ; Luke
Local $mail = Json_Get($jsonObject, '.author.mail') ; [email protected]
local $test = Json_Get($jsonObject, '.droids[1].name') ; BB8
; With array notation
Local $project2 = Json_Get($jsonObject, '["project"]')
Local $name2 = Json_Get($jsonObject, '["author"]["name"]')
Local $mail2 = Json_Get($jsonObject, '["author"]["mail"]')
local $test2 = Json_Get($jsonObject, '["droids"][1]["name"]')
从一个数组json中迭代值。
Local $file = @ScriptDir & "\assets\DROIDS.json"
Local $jsonObject = json_decode_from_file($file)
; Check if exists an item droid into the collection `droids`
Local $droids = Json_Get($jsonObject, '.droids')
If UBound($droids) = 0 Then
Return SetError(3, 0, "Array attribute 'droids' is empty.")
EndIf
; Iterate items collection to store values into an array
Local $array[UBound($droids)][3]
For $i = 0 To UBound($droids) - 1 Step 1
$array[$i][0] = Json_Get($jsonObject, '.droids' & '[' & $i & '].name')
$array[$i][1] = Json_Get($jsonObject, '.droids' & '[' & $i & '].type')
$array[$i][2] = Json_Get($jsonObject, '.droids' & '[' & $i & '].size')
Next