Compilation and analysis of XML data JSON data Litjson data

Source: Internet
Author: User

Generate the following data in XML
<?xml version= "1.0" encoding= "UTF-8"?>
<transform name= "Mycube" >
<Position>
<X>11</X>
<Y>89</Y>
<Z>20</Z>
</Position>
</Transform>

The actions are as follows in C # script

Create a document
XmlDocument doc = new XmlDocument ();
void Start () {
Creatxml ();
Create a document
void Creatxml () {
Create a declaration
XmlDeclaration Dec = doc. Createxmldeclaration ("1.0", "UTF-8", null);
Statements are stitched into the document
Doc. AppendChild (DEC);
Create a root element node
XmlNode RootNode = doc. CreateNode (XmlNodeType.Element, "Transform", null);
To add a property to a root element node
XmlAttribute attr = doc. CreateAttribute ("name");
attr. Value = "Yourcube";
RootNode.Attributes.SetNamedItem (attr);
Stitching into an XML document
Doc. AppendChild (RootNode);
Create a child element position add to the root element node and stitch it in
XmlElement pos = doc. CreateElement ("Position");
Rootnode.appendchild (POS);
XmlElement pos_x = doc. CreateElement ("X");
XmlElement pos_y = doc. createelement ("Y");
XmlElement pos_z = doc. createelement ("Z");
Pos_x.innertext = "78";
Pos_y.innertext = "18";
Pos_z.innertext = "38";
Pos. AppendChild (pos_x);
Pos. AppendChild (pos_y);
Pos. AppendChild (pos_z);
Save save Path and name
The path to the Application.datapath is the assets file//written within this folder
Doc. Save (Application.datapath + "/xml/monocodexml.xml");}

Parse the above XML data as follows

First get the location of the document, use an XML document instance to load the file
Doc. Load (Application.datapath + "/xml/monocodexml.xml");
Based on data structure to analyze if you get what you want.
The first step is to get the root element node
XmlElement root = Doc. DocumentElement;
Print (root. Name); Got the transform.
Root. FirstChild is the node of your first child element below this layer
XmlNode pos_x = root. Firstchild.firstchild;
The contents of the data stored in the X child node;
string pos_x_string = Pos_x.innertext;
Print (pos_x_string);//Printing 78
Or the same hierarchical path to get the contents of this node
XmlNode pox_x = root. selectSingleNode ("/transform/position/x");//Print 78

/////////////////////////////////////////////////////////////

Using Json to implement the following data needs to add a plugin System.json

{"Heroname": "The Hand of Noxus",
"HERO_HP": 582.24,
"Attack": 55.88,
"Skills": [{
"Key": "Q",
"SkillName": "The Building Quartet",
"CD": "9/8/7/6",
"MP": 30}, {
"Key": "W",
"SkillName": "Crippled Blow",
"CD": "9/8/7/6",
"Mp": 30}, {
"Key": "E",
"SkillName": "The Building Quartet",
"CD": "9/8/7/6",
"MP": 30},{
"Key": "R",
"SkillName": "Guillotine",
"CD": "9/8/7/6",
"MP": 30}}]}

Create a curly brace using System.json;
Jsonobject Nuoke = new Jsonobject ();
Easy to accept the skill array later
Jsonarray Skillarr = new Jsonarray ();
Jsonobject skill_q = new Jsonobject ();
Jsonobject Skill_w = new Jsonobject ();
Jsonobject skill_e = new Jsonobject ();
Jsonobject skill_r = new Jsonobject ();
Add a corresponding attribute to each JSON skill
Skill 1
Skill_q.add ("Key", "Q");
Skill_q.add ("Skill", "Big Kill quartet");
Skill_q.add ("CD", "9/8/7/6");
Skill_q.add ("MP", "30");
Skill 2
Skill_w.add ("Key", "W");
Skill_w.add ("Skill", "Crippled Strike");
Skill_w.add ("CD", "9/8/7/6");
Skill_w.add ("MP", "30");
Skill 3
Skill_e.add ("Key", "E");
Skill_e.add ("Skill", "Ruthless Iron hand");
Skill_e.add ("CD", "9/8/7/6");
Skill_e.add ("MP", "30");
Skill 4
Skill_r.add ("Key", "R");
Skill_r.add ("Skill", "guillotine");
Skill_r.add ("CD", "9/8/7/6");
Skill_r.add ("MP", "30");
Add four skills to the array
Skillarr.add (SKILL_Q);
Skillarr.add (Skill_w);
Skillarr.add (skill_e);
Skillarr.add (Skill_r);
Add Key-value in order to
Nuoke. ADD ("Heroname", "Knock Kill Hand");
Nuoke. ADD ("hero_hp", "584.23");
Nuoke. ADD ("Attack", 22.88);
Nuoke. ADD ("Skills", Skillarr);
Extended reference using System.IO;
Create a data order to write//using System.IO;
StreamWriter writer = new StreamWriter (Application.datapath + "/jsion/mysystemjson.txt");
Nuoke. Save (writer);
Auto buffering
Writer. AutoFlush = true;
Shut down
Writer. Close ();
Refresh the automatic appearance without manually refreshing the reference editor using Unityeditor;
Assetdatabase.refresh ();

Parse the above data

void Parsejsondate () {
Read from local file
FileInfo file = new FileInfo (Application.datapath + "/jsion/mysystemjson.txt");
using System.Text; to battery encoding
StreamReader reader = new StreamReader (file. OpenRead (), Encoding.UTF8);
String str = reader. ReadToEnd ();
Reader. Close ();
Freeing resources
Reader. Dispose ();
Start parsing
Heroinfo m_heroinfo = jsonutility.fromjsonPrint (M_heroinfo. Skills [3]. SkillName);}
Pathbreaker data model, accepting parsed data
In the future, we'll just point out where we need the data.
Reference using System;
[Serializable]
public class skillkv{
public string key = "";
public string Skill = "";
public string CD = "";
public string MP = "";}
[serializable]//modifier object is the byte[of uniformly serializing an object into a binary data stream]
public class heroinfo{
public string heroname = "";
public float hero_hp = 0f;
public float Attack = 0f;
Public list<skillkv> Skills = new list<skillkv> ();}

To write out with Litjson is to add a plugin Litjson

Reference using Litjson;
Create a JSON-structured data
Jsondata nuoks = new Jsondata ();
nuoks ["heroname"] = "Hand of Noxus";
nuoks ["HEROHP"] = 583.55;
nuoks ["Attack"] = 100.39;
Create a new inside containing the subsequent skills directly let ["Skills"].add ()
nuoks ["Skills"] = new Jsondata ();
Small skills
Jsondata skill_q = new Jsondata ();
skill_q ["key"] = "Q";
skill_q ["skillname"] = "big Kill quartet";
skill_q ["CD"] = "9/8/7/6";
skill_q ["MP"] = "30";
Jsondata Skill_w = new Jsondata ();
Skill_w ["key"] = "W";
Skill_w ["skillname"] = "big Kill quartet";
Skill_w ["CD"] = "9/8/7/6";
Skill_w ["MP"] = "30";
Jsondata skill_e = new Jsondata ();
skill_e ["key"] = "Q";
skill_e ["skillname"] = "big Kill quartet";
skill_e ["CD"] = "9/8/7/6";
skill_e ["MP"] = "30";
Jsondata skill_r = new Jsondata ();
Skill_r ["key"] = "R";
Skill_r ["skillname"] = "big Kill quartet";
Skill_r ["CD"] = "9/8/7/6";
Skill_r ["MP"] = "30";
nuoks ["Skills"]. ADD (SKILL_Q);
nuoks ["Skills"]. ADD (Skill_w);
nuoks ["Skills"]. ADD (skill_e);
nuoks ["Skills"]. ADD (Skill_r);
String path = Application.datapath + "/exercise/litjsonstudy.txt";
File to create a text
FileStream file = new FileStream (path, filemode.create);
The UTF8 data stream that gets the string references the using System.Text;
byte[] BTS = System.Text.Encoding.UTF8.GetBytes (nuoks. ToJson ());
File. Write (BTS, 0, BTS. Length);
if (file! = null) {
Empty buffer
File. Flush ();
Close the stream
File. Close ();
Destroying resources
File. Dispose ();}
Refresh the automatic occurrences of the reference editor using Unityeditor;
Assetdatabase.refresh ();

The analysis is as follows

StreamReader reader = new StreamReader (Application.datapath + "/exercise/litjsonstudytwo.txt");
String str = reader. ReadToEnd ();
Reader. Close ();
Reader. Dispose ();
Jsondata date = Jsonmapper.toobject (str);
Print (date ["Skills"] [0] ["skillname"]);

Compilation and analysis of XML data JSON data Litjson data

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.