메뉴 여닫기
개인 메뉴 토글
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

Autoit json

데브카페

autoit json 파일 이용

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <FileConstants.au3>
#include "Json.au3" ; Json.au3 UDF 필요

; GUI 생성
Global $hGUI = GUICreate("JSON Manager", 600, 400)
Global $hListView = _GUICtrlListView_Create($hGUI, "Key|Value", 10, 10, 580, 300)
Global $btnAdd = GUICtrlCreateButton("Add", 10, 320, 80, 30)
Global $btnEdit = GUICtrlCreateButton("Edit", 100, 320, 80, 30)
Global $btnDelete = GUICtrlCreateButton("Delete", 190, 320, 80, 30)
Global $btnSave = GUICtrlCreateButton("Save to File", 280, 320, 80, 30)
Global $inputKey = GUICtrlCreateInput("", 10, 360, 120, 20)
Global $inputValue = GUICtrlCreateInput("", 140, 360, 120, 20)

; ListView 설정
_GUICtrlListView_SetColumnWidth($hListView, 0, 200)
_GUICtrlListView_SetColumnWidth($hListView, 1, 350)

; JSON 파일 경로
Global $sJsonFile = @ScriptDir & "\data.json"
Global $oJson = ObjCreate("Scripting.Dictionary") ; JSON 데이터를 저장할 객체

; JSON 파일 로드
LoadJson()

GUISetState(@SW_SHOW)

; 메인 루프
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $btnAdd
            AddItem()
        Case $btnEdit
            EditItem()
        Case $btnDelete
            DeleteItem()
        Case $btnSave
            SaveJson()
    EndSwitch
WEnd

GUIDelete($hGUI)

; 함수들
Func LoadJson()
    _GUICtrlListView_DeleteAllItems($hListView)
    If FileExists($sJsonFile) Then
        Local $sJsonData = FileRead($sJsonFile)
        $oJson = Json_Decode($sJsonData)
        If Not @error And IsObj($oJson) Then
            Local $aKeys = $oJson.Keys()
            For $sKey In $aKeys
                _GUICtrlListView_AddItem($hListView, $sKey)
                _GUICtrlListView_AddSubItem($hListView, _GUICtrlListView_GetItemCount($hListView) - 1, String($oJson.Item($sKey)), 1)
            Next
        Else
            $oJson = ObjCreate("Scripting.Dictionary")
        EndIf
    EndIf
EndFunc

Func AddItem()
    Local $sKey = GUICtrlRead($inputKey)
    Local $sValue = GUICtrlRead($inputValue)
    
    If $sKey <> "" And $sValue <> "" Then
        If Not $oJson.Exists($sKey) Then
            $oJson.Add($sKey, $sValue)
            _GUICtrlListView_AddItem($hListView, $sKey)
            _GUICtrlListView_AddSubItem($hListView, _GUICtrlListView_GetItemCount($hListView) - 1, $sValue, 1)
            GUICtrlSetData($inputKey, "")
            GUICtrlSetData($inputValue, "")
        Else
            MsgBox(48, "Warning", "이미 존재하는 키입니다.")
        EndIf
    Else
        MsgBox(48, "Warning", "키와 값을 모두 입력해주세요.")
    EndIf
EndFunc

Func EditItem()
    Local $iSelected = _GUICtrlListView_GetSelectedIndices($hListView)
    If $iSelected <> "" Then
        Local $sKey = _GUICtrlListView_GetItemText($hListView, $iSelected, 0)
        Local $sNewValue = GUICtrlRead($inputValue)
        
        If $sNewValue <> "" Then
            $oJson.Item($sKey) = $sNewValue
            _GUICtrlListView_SetItemText($hListView, $iSelected, $sNewValue, 1)
            GUICtrlSetData($inputKey, "")
            GUICtrlSetData($inputValue, "")
        Else
            MsgBox(48, "Warning", "새 값을 입력해주세요.")
        EndIf
    Else
        MsgBox(48, "Warning", "수정할 항목을 선택해주세요.")
    EndIf
EndFunc

Func DeleteItem()
    Local $iSelected = _GUICtrlListView_GetSelectedIndices($hListView)
    If $iSelected <> "" Then
        Local $sKey = _GUICtrlListView_GetItemText($hListView, $iSelected, 0)
        $oJson.Remove($sKey)
        _GUICtrlListView_DeleteItem($hListView, $iSelected)
    Else
        MsgBox(48, "Warning", "삭제할 항목을 선택해주세요.")
    EndIf
EndFunc

Func SaveJson()
    Local $sJsonData = Json_Encode($oJson, $JSON_PRETTY_PRINT)
    Local $hFile = FileOpen($sJsonFile, $FO_OVERWRITE)
    If $hFile <> -1 Then
        FileWrite($hFile, $sJsonData)
        FileClose($hFile)
        MsgBox(64, "Success", "JSON 파일이 저장되었습니다.")
    Else
        MsgBox(16, "Error", "파일을 저장할 수 없습니다.")
    EndIf
EndFunc

Comments