請先將 Newtonsoft.Json.dll 檔案放到 Unity 專案底下,即可開始使用。
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
public class Book
{
public int ID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
}
public class Test : MonoBehaviour
{
void
Start ()
{
Book book = new Book ();
book.ID = 1;
book.Name = "Push Loli ( Getting Started )";
book.Author = "Alice";
// Json Encoding
string output = JsonConvert.SerializeObject (book);
print (output);
// Json Decoding
Book book2 = JsonConvert.DeserializeObject<Book> (output);
print ( "ID : " + book2.ID);
print ( "Name : " + book2.Name);
print ( "Author : " + book2.Author);
}
}
輸出結果:
高級版:
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Book
{
public int ID ;
public string Name ;
public string Author;
public List< string> ListData = new List< string> ();
}
public class RootLibrary
{
// Value name (Library) is Json root "Name".
public List<Book> Library = new List<Book> ();
}
public class Test : MonoBehaviour
{
void Start ()
{
// Json Path : Library.book
Book book = new Book ();
book.ID = 1;
book.Name = "Unity Book";
book.Author = "Bee"
;
book.ListData.Add ( "Box");
book.ListData.Add ( "Banana");
book.ListData.Add ( "Ball");
// Json Path : Library.book2
Book book2 = new Book ();
book2.ID = 2;
book2.Name = "C# Book";
book2.Author = "God"
;
book2.ListData.Add ( "Man");
book2.ListData.Add ( "Boy");
book2.ListData.Add ( "Girl");
// Json Path : Library.book3
Book book3 = new Book ();
book3.ID = 3;
book3.Name = "Loli Book";
book3.Author = "Alice"
;
book3.ListData.Add ( "Cat");
book3.ListData.Add ( "Dog");
book3.ListData.Add ( "Apple");
// Json Path : Library (Root)
RootLibrary lib = new RootLibrary ();
lib.Library.Add (book);
lib.Library.Add (book2);
lib.Library.Add (book3);
// 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.ListData.Count; i++) {
print ( "ListData [ " + i + " ] : "
+ _book.ListData [i]);
}
}
}
}
輸出結果(第一行因為太長了所以被切斷,所以我自行排版後並上傳文字結果):
{
"Library":
[
{
"ID":1,
"Name":"Unity Book",
"Author":"Bee",
"ListData":["Box","Banana","Ball"]
},
{
"ID":2,
"Name":"C# Book",
"Author":"God",
"ListData":["Man","Boy","Girl"]
},
{
"ID":3,
"Name":"Loli Book",
"Author":"Alice",
"ListData":["Cat","Dog","Apple"]
}
]
}
Json 查詢:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class RootLibrary
{
public string Type;
}
public class Test : MonoBehaviour
{
void
Start ()
{
RootLibrary lib = new RootLibrary ();
lib.Type = "BookLibrary";
// Json Encoding
string output = JsonConvert.SerializeObject (lib);
print (output);
//--------------------------------------------------------------
// Json Decoding
JObject obj = JsonConvert.DeserializeObject<JObject> (output);
print (obj.GetValue( "Type"
));
print (obj[ "Type"]);
}
}
輸出結果:
原文出處:Unity C# Json 編碼、解碼 (使用 Json.Net) @ 彥霖 實驗筆記 :: 痞客邦 PIXNET ::