Unity ゲームの C# で、JSON の配列を一種の配列 (おそらくリスト?) に変換しようとしています。これを行うために考えられるすべてのことを試みましたが、成功しませんでした。配列なしで変換しようとしている JSON の例を次に示します。
[
{
"id": 0,
"name": "Name 0",
"description": "Description for id 0 goes here."
},
{
"id": 1,
"name": "Name 1",
"description": "Description for id 1 goes here."
}
]
そして、これをC#でリストに変換する方法は次のとおりです。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
public class BuildingSystem : MonoBehaviour {
private List<BuildObjects> database = new List<BuildObjects>();
private JsonData buildingData;
void Start() {
buildingData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Buildings.json"));
ConstructBuildingDatabase();
}
void ConstructBuildingDatabase() {
for (int i = 0; i < buildingData.Count; i++) {
database.Add (new BuildObjects ((int)buildingData [i] ["id"],
(string)buildingData [i] ["name"],
(string)buildingData [i] ["description"]));
}
}
}
public class BuildObjects {
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public BuildObjects (int id, string name, string description) {
this.ID = id;
this.Name = name;
this.Description = description;
public BuildObjects() {
this.ID = -1;
}
}
たとえば、次のように JSON に新しい変数を追加したい場合:
[
{
"id": 0,
"name": "Name 0",
"description": "Description for id 0 goes here.",
"properties": [{"bool": true, "string": "text goes here"},{"bool": false, "string": "more text goes here"}]
}
]
C# スクリプトでどのように読み取ることができますか? 「プロパティ」を定義しようとしました(この行で
(type)buildingData [i] ["properties"]
) bool[]、新しい public クラスの Properties を持つ List (私が立ち往生した) として、そして必死になって ArrayList と BitArray を作成しました。
しかし、これらの方法でそれを行うのに失敗したことであなたを思いとどまらせないでください.これらの方法の1つとしてそれを行う方法を知っていると信じているなら、私はおそらく間違って試しました. 私はこれにかなりこだわっています。大歓迎です!