How do I return a series of arrays from a SimpleJSON Object
I'm looking to Load (and later save) a variation of data from a SimpleJSON Object.
The data set has a finite number of entries for the final project. Here is a snippet from the JSON file:
{
"ZJEquipmentItems" :
[
{
"Equip_Name" : "AR 01",
"Equip_Level": "1",
"Equip_Damage" : "100",
},
{
"Equip_Name" : "AR 02",
"Equip_Level": "0",
"Equip_Damage" : "110",
},
{
"Equip_Name" : "AR 03",
"Equip_Level": "0",
"Equip_Damage" : "115",
},
]
}
I want to return 3 Arrays or possibly Lists (not sure which approach makes the most sense).
ENames would be an array of Type string, ELevels would be an array of Type int, and EDamValues would be an array of Type float.
My LoadData calls that needs some love:
public void LoadDataFromFile()
{
string path = "Assets/_TempData/ZJEquipmentData.json";
string jsonString = File.ReadAllText(path);
JSONObject inventoryJson = (JSONObject)JSON.Parse(jsonString);
STUCK HERE
}
Any recommendations?
Answer by NataTooma · Sep 13, 2020 at 11:51 PM
Here is how I finally worked it all out, in case anyone else is looking for this information.
using UnityEngine;
using System.IO;
using SimpleJSON;
public class EquipmentData : MonoBehaviour
{
public TextAsset jsonAsset;
public string[] Equip_Names;
public string Equip_Name;
public int[] Equip_Levels;
public int Equip_Level;
public float[] Equip_DamValues;
public float Equip_Damage;
public float[] Equip_RangeValues;
public float Equip_Range;
public void Load_JSON_Equipment_Data()
{
string path = "Assets/_TempData/EquipmentData.json";
string jsonString = File.ReadAllText(path);
JSONObject inventoryJSON = (JSONObject)JSON.Parse(jsonString);
JSONNode node;
for (int i = 0; i < Equip_Names.Length; i++)
{
node = inventoryJSON["ZJEquipmentItems"];
Equip_Names[i] = node[i]["Equip_Name"];
Equip_Levels[i] = node[i]["Equip_Level"];
Equip_DamValues[i] = node[i]["Equip_Damage"];
Equip_RangeValues[i] = node[i]["Equip_Range"];
}
}
}
$$anonymous$$ake sure to initialize the size of each array.
Your answer
Follow this Question
Related Questions
File.ReadAllBytes() returns byte[0] 0 Answers
Array not working properly 0 Answers
Location for files in order to be able to load them on Android 2 Answers
JSON parse error 0 Answers
[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer