LoginSignup
6
3

More than 5 years have passed since last update.

【Unity】JSONを整形するフォーマットクラス

Last updated at Posted at 2018-03-11

はじめに

json-formatterはJSONを整形するフォーマットクラスです。

json-formatter

導入方法

json-formatter (GitHub)からダウンロードして、インポートしてください。

使用例

使用例
using UnityEngine;
using System;
using System.IO;
using HC.Common;


public class Example : MonoBehaviour
{
    #region イベントメソッド

    private void Start()
    {
        string json = ReadText("JsonFormatter/Demo/example.json");

        json = JsonFormatter.ToMinifyPrint(json);
        Debug.Log("MinifyPrint: " + Environment.NewLine + json);

        json = JsonFormatter.ToPrettyPrint(json, JsonFormatter.IndentType.Space);
        Debug.Log("PrettyPrint: " + Environment.NewLine + json);
    }

    #endregion


    #region メソッド

    /// <summary>
    /// テキストをファイルから読み込む
    /// </summary>
    /// <param name="path">Application.dataPath以降のファイルパス</param>
    /// <returns>読み込んだテキスト</returns>
    public static string ReadText(string path)
    {
        string text = string.Empty;

        try
        {
            using (var streamReader = new StreamReader(Path.Combine(Application.dataPath, path)))
            {
                text = streamReader.ReadToEnd();
                streamReader.Close();
            }
        }
        catch (Exception e)
        {
            Debug.LogAssertion(e.Message);
        }

        return text;
    }

    #endregion
}
MinifyPrint
{"characters":[{"id":1,"name":"武闘家","EquipWeaponTypes":["片手剣","両手剣","槍","斧"],"hp":140,"mp":40,"power":80,"intelligence":20,"agility":14.0},{"id":2,"name":"遊び人","EquipWeaponTypes":[],"hp":60,"mp":0,"power":20,"intelligence":80,"agility":8.0}]}
PrettyPrint
{
    "characters": [
        {
            "id": 1,
            "name": "武闘家",
            "EquipWeaponTypes": [
                "片手剣",
                "両手剣",
                "槍",
                "斧"
            ],
            "hp": 140,
            "mp": 40,
            "power": 80,
            "intelligence": 20,
            "agility": 14.0
        },
        {
            "id": 2,
            "name": "遊び人",
            "EquipWeaponTypes": [],
            "hp": 60,
            "mp": 0,
            "power": 20,
            "intelligence": 80,
            "agility": 8.0
        }
    ]
}

配布ライセンス

MIT

6
3
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
6
3