茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729341 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00087.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Dot Net? : [轉貼]C# 產生 JSON 字串的幾種方式整理

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity C# Json 編碼、解碼 (使用 Json.Net)

Unity C# Json 編碼、解碼 (使用 Json.Net)

請先將 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);

        }
}

 輸出結果:

Unity - Untitled - JsonTest - PC, Mac & Linux Standalone  

 


 高級版:

 


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"]
              }
       ]
}

  Unity - Untitled - JsonTest - PC, Mac & Linux Standalone  

 


 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 - Untitled - JsonTest - PC, Mac & Linux Standalone  


原文出處:Unity C# Json 編碼、解碼 (使用 Json.Net) @ 彥霖 實驗筆記 :: 痞客邦 PIXNET ::
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|