Read JSON file data online There are many ways to use Simplejson, the introduction of Simplejson refer to the following links: Http://wiki.unity3d.com/index.php/SimpleJSON, Import the relevant files for Simplejson before you use them.
JSON file name: Achieve.json, File contents:
{"30002": {"achieve_id": 30002, "achieve_name": "Streak", "Achieve_race": 3, "Achieve_type": $, "achieve_des": " Qualifying consecutive victories 10 "," Achieve_gold ":" Achieve_times ": Ten," image ":" Athletics_2 "}," 30003 ": {" achieve_id ": 30003," Achieve_name ":" Victorious "," Achieve_race ": 3," Achieve_type ": $," achieve_des ":" Qualifying consecutive victories 100 "," Achieve_gold ": 100," Achieve_times ":", "image": "Athletics_3"},"30004": {"achieve_id": 30004, "achieve_name": "Unyielding", "achieve_ Race ": 3," Achieve_type ":," Achieve_des ":" Qualifying consecutive failure 10 "," Achieve_gold ": 5," Achieve_times ": Ten," image ":" Athletics_4 " } }
View Code
The script code is posted directly here:
Configachieve Class Code:
usingUnityengine;usingSystem.Collections;usingSimplejson; Public classconfigachieve{PrivateHashtable hash =NewHashtable (); //load information from JSON file to Hashtable Public voidLoadfromjson (Jsonnode data) {if(Data = =NULL) Debug.Log ("Read Faild"); Else { for(intI=0; I<data. Count; i++) {configachieveitem node=NewConfigachieveitem (); Node. Id= data [i] ["achieve_id"]; Node. Name= data [i] ["Achieve_name"]; Node. Race= data [i] ["Achieve_race"]; Node. Type= data [i] ["Achieve_type"]; Node. Des= data [i] ["Achieve_des"]; Node. Gold= data [i] ["Achieve_gold"]; Node. Times= data [i] ["Achieve_times"]; Node. Image= data [i] ["Image"]; Hash. ADD (node. ID, node); } } } //returns the value of key from Hashtable PublicConfigachieveitem GetItem (stringkey) {Configachieveitem Itemachieve=(Configachieveitem) Hash[key]; returnItemachieve; }}
Configachieve
Configachieveitem Class Code:
usingSystem;usingSimplejson; Public classconfigachieveitem{Private string_configid =""; Private string_configname =""; Private string_configrace =""; Private string_configtype =""; Private string_configdes =""; Private string_configgold =""; Private string_configtimes =""; Private string_configimage =""; Public stringId {Get{return_configid;} Set{_configid =value;} } Public stringName {Get{return_configname;} Set{_configname =value;} } Public stringRace {Get{return_configrace;} Set{_configrace =value;} } Public stringType {Get{return_configtype;} Set{_configtype =value;} } Public stringDes {Get{return_configdes;} Set{_configdes =value;} } Public stringGold {Get{return_configgold;} Set{_configgold =value;} } Public stringTimes {Get{return_configtimes;} Set{_configtimes =value;} } Public stringImage {Get{return_configimage;} Set{_configimage =value;} } Public voidLoaddatafromjson (Configachieve Achieve,stringkey) {Configachieveitem temp=(Configachieveitem) achieve. GetItem (key); _configid=temp. Id; _configname=temp. Name; _configrace=temp. Race; _configtype=temp. Type; _configdes=Temp._configdes; _configgold=temp. Gold; _configtimes=temp. times; _configimage=temp. Image; }}
Configachieveitem
ConfigManager Class Code:
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingUnityengine;usingSimplejson; Public classconfigmanager:monobehaviour{ PublicConfigachieve _config_achieve =NewConfigachieve (); //Use this for initialization voidStart () {Test ();//test function } //Update is called once per frame voidUpdate () {}//Open the JSON file for the corresponding path PublicJsonnode Jsonfromfile (stringfilename) {Jsonnode Json_object=NULL; Textasset FilePath=(textasset) resources.load (filename); if(FilePath = =NULL)//JSON file read failed, print logDebug.Log ("find JSON file faild!"); ElseJson_object= JSON. Parse (Filepath.text);//convert to JSON format returnJson_object; } Public voidTest () {_config_achieve.loadfromjson (Jsonfromfile ("Config/achieve")); Configachieveitem Temp= _config_achieve. GetItem ("10001"); Debug.Log ("id ="+ Temp. Id +"name ="+temp. Name); Debug.Log ("race ="+ Temp. Race +"type ="+temp. Type); Debug.Log ("des ="+ Temp. Des +"Gold ="+temp. Gold); Debug.Log ("Times ="+ Temp. Times +"image ="+temp. Image); }}
ConfigManager
Where ConfigManager is used to control the opening of the JSON file, Configachieve is used to read the data, Configachieveitem is used to save each piece of data and then you can use the test function in the code to test the read correctly. (For a long time, probably this way, the specific code to prevail) ~ ~