using System;
using SimpleJSON;
using System.IO;
namespace JsonTets1
{
class Program
{
static void Main(string[] args)
{
int myInt = 32;
string myString = "Mamma mia!";
bool myBool = true;
int[] myIntArr = new int[3] { 1, 2, 3 };
bool[,] myBoolArr = new bool[2, 2] { { true, false }, { false, false } };
JSONNode j = JSONNode.Parse("{}");
j["int"] = myInt;
j["string"] = myString;
j["bool"] = myBool;
j["intArr"] = myIntArr; //error here >> Cannnot implicitly type 'int[]' to 'SimpleJSON.JSONNode'
j["boolArr"] = myBoolArr; //error here >> Cannnot implicitly type 'bool[*,*]' to 'SimpleJSON.JSONNode'
Console.Write(j.ToString());
using (StreamWriter f = new StreamWriter("new1.txt"))
{
f.Write(j.ToString());
}
}
}
}
I don’t have much affinity with this library, I have tried to use it to save bool [,] and int data in a unity project, but i get the error you can read in the script above, so I created a simple script to study it more closely, but I still can’t understand how to do it: I tried to create functions to encode arrays into strings and then decode them, but this is quite complex and inconvenient; Then I realized that there is a class called JSONArray, but the problem is the same, for the assignment a value of type JSONNode is required (apparently int, string and bool are implicitly converted); on the internet I found some solutions: Json.Deserialize, JsonConvert etc. None of these are compatible with my library.
At the end, how do I store arrays in json?
Link to my Google Drive for Json libraries (in the script I only use the one called SimpleJSON):
https://drive.google.com/drive/folders/1XIy9enfX41biQ7MBGStF4LG7yW9Ez6WY?usp=sharing
@Bunny83
Sorry for my ignorance about SimpleJSON library but the variable JSON j is an array?
because you need to specify the element as int index [0].
j[index] = myIntArr[index];
You have the wrong idea about how the library works ^^. Most json libraries are object mappers which work with POCOs. So you have to actually create an object structure in C# that represents the json object structure. SimpleJSON does not do object mapping to any POCO objects. It just represents the json object structure one to one with its own simple classes. So every value in the json text is represented through a JSONNode value. The implicit conversions only work for primitive types. So there is no direct conversion from a JSONArray to a native C# / .NET array. However you can of course copy the data yourself if you really need it in a native array.
Note that multidimensional arrays in .NET / C# have some limitations. They always need to be rectangular. Json does not have multi dimensional arrays but only jagged arrays. The problem is json can represent any multidimensional array but a multidimensional C# array may not necessarily be able to represent a json jagged array since the sub array are not required to have the same size.
So what you can do is something like this:
JSONNode j = new JSONObject();
j["int"] = myInt;
j["string"] = myString;
j["bool"] = myBool;
var intArr = j["intArr"].AsArray;
foreach(var val in myIntArr)
intArr.Add(val);
var boolArr = j["boolArr"].AsArray;
int size0 = myBoolArr.GetLength(0);
int size1 = myBoolArr.GetLength(1);
for (int i = 0; i < size0; i++)
{
var subArr = boolArr[-1].AsArray;
for (int j = 0; j < size1; j++)
{
subArr.Add(boolArr[i,j]);
}
}
Note that var subArr = boolArr[-1].AsArray;
is just a little trick to essentially do
var subArr = new JSONArray();
boolArr.Add(subArr);
The rest should be pretty straight forward. Note if you need to serialize / deserialize multi dimensional arrays a lot, of course you can implement your own conversion extension. However you would need to implement every required conversion yourself which is probably not worth the efford.
SimpleJSON is a simple and lightweight json library. You have full control over the structure and you are not bound to a certain class structure like you are with most object mappers.