Unity 使用JsonFx解析本地json数据

Unity 使用JsonFx解析本地json数据


DLL文件
http://yunpan.cn/cHhQVVgSDD4zR  访问密码 2c82

test_json.txt文件
{
    "name": "ALLPlayersList",
    "players": [
        {
            "name": "guodong1",
            "age": 23
        },
        {
            "name": "guodong2",
            "age": 24
        }
    ]
}

将test_json.txt文件放在Assets\testJson\文件夹下

测试代码
using UnityEngine;
using System.Collections;
using JsonFx.Json;
using System;
using System.IO;


public class PersonList
{
    public string name;
    public Person[] players;
}


public class Person
{
    public string name;
    public int age;
}

public class testJson : MonoBehaviour {


    
	// Use this for initialization
	void Start () {
	    //Console.WriteLine(" test Json start");
        print("test Json start");
        string fileName = "Assets/testJson/test_json.txt";
        if (File.Exists(fileName))
        {
            StreamReader sr = File.OpenText(fileName); 
            string input = sr.ReadToEnd();

            PersonList personList = JsonReader.Deserialize<PersonList>(input);
            print("personListName = " + personList.name);
            //Person[] persons = JsonReader.Deserialize<Person[]>(input);


            for (int i = 0; i < personList.players.Length; i++)
            {
                Person currenttPerson = personList.players[i];
                print("person name = " + currenttPerson.name + "person age = " + currenttPerson.age);
            }
           
        }

        

	}
	
	// Update is called once per frame
	void Update () {
	
	}
}


运行结果:
Unity 使用JsonFx解析本地json数据_第1张图片



你可能感兴趣的:(Unity 使用JsonFx解析本地json数据)