2015年11月29日 星期日

Unity for Json


Download Newtonsoft.Json



Testjson (C#):  寫入資料

using UnityEngine;
using System.Collections;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;

/**
  * Book Class set Date ID, Name, Author.
    using System.Collections.Generic class, user liset class 
  */
public class Book
{
    public int ID;       
    public string Name;
    public string Author;

    public List<string> listDate = new List<string>(); 
}

/**
  * RootLibrary is add Date
*/
public class RootLibrary{
    public List<Book> Library = new List<Book>();
}

public class Testjson : MonoBehaviour {

void Start () {
        
        // set Date Book class value
        Book book = new Book();
        book.ID = 1;
        book.Name = "Bee";
        book.Author = "bee";
        
        // list add Date value
        book.listDate.Add("Box");
        book.listDate.Add("Bananana");
        book.listDate.Add("Ball");

        // set Date Book2 calss value is string
        Book book2 = new Book();
        book2.ID = 2;
        book2.Name = "Cat";
        book2.Author = "cat";

        // list add Date value is string
        book2.listDate.Add("Box2");
        book2.listDate.Add("Test222") ;
        book2.listDate.Add("Back");

        // RootLibrary add book class Date
        RootLibrary lib = new RootLibrary();
        lib.Library.Add(book);
        lib.Library.Add(book2);

        // Json Encoding
        string outPut = JsonConvert.SerializeObject(lib);
        print(outPut);

        // Json Decoding
        RootLibrary _lib = JsonConvert.DeserializeObject<RootLibrary>(outPut);

        foreach (Book _book in _lib.Library) {
            print("--------------");
            print("ID"+ _book.ID);
            print("Name" + _book.Name);
            print("Author:" +_book.Author);

            for (int i =0; i<_book.listDate.Count; i++) {
                print("ListDate["+i+"]:" + _book.listDate[i]);
            }
        }
    }
}

結果圖: 

























----------------------------------------------

TestjsonInquire (c#) : 收尋資料

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// Root_Library calss set value
public class Root_Library{
    public string Type;
}

public class TestjsonInquire : MonoBehaviour {

    void Start() {

        Root_Library lib = new Root_Library();
        lib.Type = "BookLibaray";

        /**
          * Json Encoding
          *  Json set vlaue Type
        */
        string output = JsonConvert.SerializeObject(lib) ;
        print(output);

        /**
          * Json Decoding 
          * Json Inquire Type value
         */
        JObject obj = JsonConvert.DeserializeObject<JObject>(output);
        print(obj.GetValue("Type"));
        print(obj["Type"]);
    }
}

結果圖:




















----------------------------------------------

Json、Bson 綜合應用:

using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;

public class Test : MonoBehaviour {

void Start () {

        var info = new List<object> {
            new {
                Date = DateTime.Now,
                name = "Moe Loli A",
                Age = 5,
                NumberA = new int[]{000,111,222},
                NumberB = new List<int> {2, 1, 0}
            },
            new {
                Date = DateTime.Now.AddDays(1),
                name = "Moe Loli B",
                Age = 7,
                NumberA = new int[]{333,444,555},
                NumberB = new List<int> {5, 4, 3}
            },
        };

        var data = new {info};

        string json = JsonConvert.SerializeObject(data, Formatting.Indented);
        JObject Jobj = JsonConvert.DeserializeObject<JObject>(json);
        print("Json: "+json);

        JArray ja = Jobj["info"] as JArray;
        foreach (JObject obj in ja) {
            DateTime date = (DateTime)obj["Date"];
            print("Json: "+data);
        }

        byte[] bson = new byte[] { };
        using (MemoryStream steam = new MemoryStream()) {
            using (BsonWriter writer = new BsonWriter(steam)) {
                new JsonSerializer().Serialize(writer, data);
            }
            bson = steam.ToArray();
        }

        JObject Bobj;

        using (var steam = new MemoryStream(bson)) {
            using (BsonReader reader = new BsonReader(steam)) {
                JsonSerializer serializer = new JsonSerializer();
                Bobj = serializer.Deserialize<JObject>(reader);
            }
        }
        print("Bson: "+ Bobj);

        JArray ja2 = Bobj["info"] as JArray;
        foreach (JObject obj in ja2) {
            DateTime date = (DateTime)obj["Date"];
            print("Bson: "+date);
        }
    }
}

結果圖: 


























沒有留言:

張貼留言