Article directory
Plugin introduction
- introduce
Json.Net is a .Net framework that reads and writes Json with high efficiency. .Json.Net makes it easier to use Json in the .Net environment. You can quickly read and write Json through Linq To JSON, and serialize your .Net objects through JsonSerializer. Let you easily realize the conversion of all types (objects, basic data types, etc.) and Json in .Net.
- Features
High performance: Faster than .NET's built-in JSON serializer
Writes indented, easy-to-read JSON
JSON and XML can be interconverted
Supports: net Standard 2.0, . net 2,. net 3.5,. net 4,. net 4.5, Silverlight, Windows Phone and Windows 8 Store
Unity uses Newtonsoft.Json for xml and json conversion
Build a demo scene
How to write a script to realize the mutual conversion between json and xml
test script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using UnityEngine.UI;
public class test : MonoBehaviour
{
public Button JSONwriterBtn;
public Button XMLwriterBtn;
private string xmlPath;
private string jsonPath;
public Text xmlText;
public Text jsonText;
XmlDocument document;
private void Start()
{
xmlPath = Application.dataPath + "/data/question.xml";
jsonPath = Application.dataPath + "/data/question.json";
document = new XmlDocument();
document.Load(xmlPath);//解析xml数据
xmlText.text = document.InnerXml;
JSONwriterBtn.onClick.AddListener(XmlToJson);
XMLwriterBtn.onClick.AddListener(JsonToXml);
}
/// <summary>
/// xml转json的方法
/// </summary>
public void XmlToJson()
{
string jsonStr = Newtonsoft.Json.JsonConvert.SerializeXmlNode(document, Newtonsoft.Json.Formatting.Indented,false);//数据序列化为Json字符串
jsonText.text = jsonStr;
System.IO.File.WriteAllText(jsonPath, jsonStr);//将转换过来的json数据写入json文件
}
/// <summary>
/// json转xml的方法
/// </summary>
public void JsonToXml()
{
string path= Application.dataPath + "/data/toxml.xml";
XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonText.text, "");//json数据反序列化为xml数据
xml.Save(path);
}
}
The script first loads the question.xml data into the XmlDocument object, then calls the Newtonfost.JSON package method JsonConvert.SerializeXmlNode to convert the xml document into a text string in json format, and then displays it on the Text, and writes the json data through the IO stream into the question.json file, and then convert the json string to an xml string and save it to the toxml.xml file.
Here I mount it on the panel, remember to drag and drop the object, otherwise it will be empty!
demo demo effect
Original xml file:
Converted json file:
Converted xml file:
demo source project
The demo download
Newtonsoft.Json plug-in can be found in the demo project. If you have any questions, you can ask questions. If you like, remember to connect three times with one click! Thanks for following.
About the acquisition of Newtonsoft.Json plugin and some common methods
Plugin acquisition
1. Obtain the official website address of the Newtonsoft.Json plugin from the official website. For
more functions, you can also view the official documentation:
Official Documentation
2. The demo project used in this article has the Newtonsoft.Json plugin, but it may not be the latest.
3. In Visual Studio, it can be obtained through NuGet Package Manager:
In the Visual Studio menu, go to Tools/NuGet Package Manager/Manager NuGet Packages for Solution. Select the Newtonsoft.json package, choose the version you need and install it for your unity project.
Common method
- Object serialize Json string:
JsonConvert.SerializeObject(object value): Deserialize
Json strings to objects:
JsonConvert.DeserializeObject(string value):
JsonConvert.DeserializeObject(string value)
- XML serialization to Json string:
JsonConvert.SerializeXmlNode(XmlNode node)
Deserialize Json to XML object:
JsonConvert.DeserializeXmlNode(string value)
- The parameter formatting refers to whether the Json string format has indentation