Using Newtonsoft.json in Unity to generate and read JSON

Source: Internet
Author: User

The first thing you need to reference in your project: Newtonsoft.Json.dll

Add Namespaces in your code: using Newtonsoft.json;

Two important methods and examples of JSON serialization and deserialization:

Jsonconvert.serializeobject (object value) serialization, which has an overloaded method Jsonconvert.serializeobject (object value, params jsonconverter [] converters)

Jsonconvert.deserializeobject (string value, type type) is deserialized, and it has an overloaded method Jsonconvert.deserializeobject (string value, type Type, params jsonconverter[] converters)

First, we first build a person class code as follows:
public class person{private string name;    public string Name {get {return Name;}  set {name = value;}  } private int age;    public int Age {get {return-age;}  set {age = value;} }}

1. Serialization

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using Newtonsoft.json; namespace jsonnet{public    partial class Test:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {person person            = new person ();            Person. Name = "Goldeneasy";            Person. Age =;            String Strserializejson = Jsonconvert.serializeobject (person);            Response.Write (Strserializejson);}}}    

Output: {"Name": "Goldeneasy", "Age": 25}

2. Deserialization

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using Newtonsoft.json; namespace jsonnet{public    partial class Test:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {person person            = new person ();            Person. Name = "Goldeneasy";            Person. Age =;            String Strserializejson = Jsonconvert.serializeobject (person);                       Person user = (person) jsonconvert.deserializeobject (Strserializejson, typeof (person));            Response.Write (user. Name);}}}    

Output Result: Goldeneasy

You can also use a template to get a JSON file by name

  private static T parsejson<t> (string filename)    {        try        {            var filePath = configs.getpath (filename) ;            if (! File.exists (FilePath)) return default (T);            var content = File.readalltext (filePath);            return jsonconvert.deserializeobject<t> (content);        }        catch (Exception ex)        {            Logger.Log ("File Parse" + ex.) Message);            return default (T);        }    }

  

Using Newtonsoft.json in Unity to generate and read JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.