Skip to content

Instantly share code, notes, and snippets.

@yuiyoichi
Last active July 5, 2016 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuiyoichi/792af1cb87011e1f648956826c0d0aa0 to your computer and use it in GitHub Desktop.
Save yuiyoichi/792af1cb87011e1f648956826c0d0aa0 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// クラスの配列をJsonUtilityでデシリアライズするヘルパー
/// http://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/
/// </summary>
public class JsonHelper {
/// <summary>
/// 配列になっているJSONデータをデシリアライズする
/// ex)YouObject[] objects = JsonHelper.getJsonArray<YouObject> (jsonString);
/// </summary>
/// <typeparam name="T">配列になっているデータの型</typeparam>
/// <param name="json">json文字列</param>
/// <returns>FromJsonで生成されたオブジェクトの配列</returns>
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[Serializable]
private class Wrapper<T>
{
#pragma warning disable 649
public T[] array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment