blog games developers documentation portfolio gallery

Documentation:

JsonString()

To export a Hashtable (with underlying hierarchy) or ArrayList to JSON, you can call the class extensions for Hashtable and ArrayList. You need to include the line
using SimpleXmlExtensions;
in your source code.

string JsonString()


This function is defined for Hashtable and for ArrayList and returns a JSON formatted string.

Since the JsonString() function only returns a string, here's an example of writing the JSON string to a file.

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

using SimpleXmlExtensions;

public class MyExporter {

public void ExportJson(Hashtable aHash) {
string jsonExportString = aHash.JsonString();
ExportToFile(jsonExportString, "SimpleJsonExport.json");
}

private void ExportToFile(string exportString, string path) {
if(exportString == null || path == null) return;
if(Application.platform==RuntimePlatform.OSXPlayer ||
Application.platform==RuntimePlatform.WindowsPlayer &&
Application.platform!=RuntimePlatform.LinuxPlayer || Application.isEditor) {

// We use UTF-8 encoding
byte[] bytes = new UTF8Encoding(true).GetBytes(exportString);
if(File.Exists(path)) File.Delete(path); // delete if it exists
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryWriter w = new BinaryWriter(fs);
w.Write(bytes);
w.Close();
fs.Close();
}
}
}







follow us