2017-03-03 5 views
0

私はJSONを吐き出すゲーム、用タイルセットを作成するためのプログラム(タイル)を使用していたが、これに類似したファイル私が使用できるデータ(「データ」のマップ、または「データ」のint [] []、幅/高さ、他のすべての情報は無関係で、私はすでに知っています)それについてどうやって行くのか本当に分かりません。C#のターンJSONにデータ

JSONからデータに変換するにはどうしたらよいですか?

答えて

1

作成するモデルクラスを使用して、所有しているJSONデータを表す必要があります。次に、JavaScriptSerializerまたはNewsoft JOSNライブラリを使用して、このデータをオブジェクトに変換できます。

あなたはあなたのデータのためのモデルクラスを作成する必要があります。

public class Layer 
{ 
    public List<int> data { get; set; } 
    public int height { get; set; } 
    public string name { get; set; } 
    public int opacity { get; set; } 
    public string type { get; set; } 
    public bool visible { get; set; } 
    public int width { get; set; } 
    public int x { get; set; } 
    public int y { get; set; } 
} 

public class Tileset 
{ 
    public int columns { get; set; } 
    public int firstgid { get; set; } 
    public string image { get; set; } 
    public int imageheight { get; set; } 
    public int imagewidth { get; set; } 
    public int margin { get; set; } 
    public string name { get; set; } 
    public int spacing { get; set; } 
    public int tilecount { get; set; } 
    public int tileheight { get; set; } 
    public int tilewidth { get; set; } 
} 

public class Data 
{ 
    public int height { get; set; } 
    public List<Layer> layers { get; set; } 
    public int nextobjectid { get; set; } 
    public string orientation { get; set; } 
    public string renderorder { get; set; } 
    public int tileheight { get; set; } 
    public List<Tileset> tilesets { get; set; } 
    public int tilewidth { get; set; } 
    public int version { get; set; } 
    public int width { get; set; } 
} 

これが完了すると、あなたがこのオブジェクトに文字列データを解析するNewtonsoft.Jsonライブラリを使用することができます。

string text = "<Your Json data>"; 
var result = JsonConvert.DeserializeObject<Data>(text); 

ここからNewtonsoft JSONライブラリをダウンロードすることができます。

http://www.newtonsoft.com/json

または同様NPMを使用します。

は、インストール・パッケージNewtonsoft.Json

+1

と思います:あなたは、オブジェクト/モデルを宣言した後、あなたがパッケージを取得するには

string Json = ""; // your Json string var Result = JsonConvert.DeserializeObject<YourModel>(Json); 

ようにそれを使用することができ、その後、あなたはNugget関数とタイプを使用することができます?たぶんいくつかの構文ですか? JavaScriptSerializerまたはNewsoft JSONライブラリの方がいいでしょうか? – user183106

1

あなたはNewtonsoft.Jsonを使用することができます。あなたはもう少し説明できる

Install-Package Newtonsoft.Json