1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jsonファイル読み書き

1
Posted at

■json

**sample.json**

{
  "General": {
    "Name": "MyApplication",
    "Version": "1.0"
  },
  "Display": {
    "Width": 1280,
    "Height": 720,
    "FullScreen": false
  }
}

■jsonファイル読み込み

function data = readJsonFile(filename, defaultData)
    if nargin < 2
        defaultData = struct();
    end

    if ~isfile(filename)
        data = defaultData;
        return;
    end

    try
        text = fileread(filename);
        data = jsondecode(text);

        % data.General.Name
        % data.General.Version
        % data.Display.Width
        % data.Display.Height
        % data.display.FullScreen
    catch
        data = defaultData;
    end
end

■jsonファイル書き込み

function writeJsonFile(filename, data)
    jsonText = jsonencode(config, PrettyPrint=true);

    fid = fopen(filename, "w", "n", "UTF-8");
    if fid == -1
        error("ファイルを作成できません: settings.json");
    end

    fprintf(fid, "%s", jsonText);
    fclose(fid);
end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?