P31RestKit.dll 2.0 Unity3d json

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using Prime31;

public class JsonTest : MonoBehaviour 
{
	// Use this for initialization
	void Start () {
        Player player = new Player()
        {
            Name = "Lee",
            Age = 19
        };


        string jsonString = Json.encode(player);

        Debug.Log(jsonString);

        player = Json.decode<Player>(jsonString);

        Debug.Log(player.Name);
        Debug.Log(player.Age);

	    List<Player> playerList = new List<Player>()
	    {
	        new Player() {Name = "wang", Age = 12},
	        new Player() {Name = "wang2", Age = 13},
	        new Player() {Name = "wang3", Age = 14},
	        new Player() {Name = "wang4", Age = 15},
	        new Player() {Name = "wang5", Age = 16},
	    };

	    string playerListString = Json.encode(playerList);

        Debug.Log(playerListString);

	    List<Player> playerList2;
	    playerList2 = Json.decode<List<Player>>(playerListString);

	    foreach (var p in playerList2)
	    {
            Debug.Log(p.Name);
            Debug.Log(p.Age);
	    }
	}
	
}

public class Player
{
    public string Name;
    public int Age;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

posted @ 2015-08-13 15:30  zfyouxi  阅读(444)  评论(0编辑  收藏  举报