Outputting data to JSON file csharp (unity) - json

I can't figure out how to output a complex data type to JSON.
I constructed a data type which basically holds smaller data types, I have also assigned the data types to new data types so they all seem to have a reference. I have looked into outputting complex data but don't seem to be able to find a problem similar to mine. I will consider appending data but this method will be much simpler if I can output the data type successfully.
Save Data Code
[System.Serializable]
public class SaveData
{
public MapData mapData;
}
[System.Serializable]
public class TileData
{
public List<BlockData> blockData;
}
[System.Serializable]
public class BlockData
{
public Vector3 blockPosition;
public string blockName;
public float blockOrientation;
public int blockLayer;
}
[System.Serializable]
public class MapData
{
public List<TileData> tileData;
}
Get Map Data Method
SaveData GetMapData()
{
mapHeight += mapStartY;
maplength += mapStartX;
int tileCounter = 0;
MapData mapData = new MapData();
SaveData saveData = new SaveData();
List<TileData> tileList = new List<TileData>();
for (float r = mapStartY; r < mapHeight; r++)
{
for(float c = mapStartX; c < maplength; c++)
{
Vector2 currentPosition = new Vector2(c * (blocksize)-(blocksize/2), blocksize * r -(blocksize/2));
GameObject[] currentTile = getObjectID.RayDetectAll(currentPosition);
if (currentTile!= null)
{
//adds a tiledata list here if the tile is occupied.
TileData tileData = new TileData();
//adds a list of blocks here.
List<BlockData> blocks = new List<BlockData>();
for (int i = 0; i < currentTile.Length; i++)
{
BlockData blockData = new BlockData();
GameObject currentBlock = currentTile[i];
blockData.blockPosition = currentBlock.transform.position;
blockData.blockName = currentBlock.name;
blockData.blockOrientation = currentBlock.transform.eulerAngles.z;
blockData.blockLayer = currentBlock.GetComponent<SpriteRenderer>().sortingOrder;
//adds a blockdata to the blocks list
blocks.Add(blockData);
Debug.LogWarning(blockData.blockName);
}
//need to assign tile data and add a new one to the list
tileList.Add(tileData);
//assins the blocks to tile data block data list
tileData.blockData = blocks;
}
else
{
//Debug.LogWarning("warning! no objects found on tile: " + currentPosition);
}
tileCounter++;
}
}
'''
I want the file to output all the data so that i can read the data and reassign it. Right now it outputs the data wrong.

Generally I think this is something that would be commented, but I can't comment yet.
If all you want is to convert an object to Json, could you use JsonUtility.ToJson() as described here?

just to let people know I devised a new method which counted an array of all tiles and assigned it to a data type with an array in it. It managed to load from this format.

Related

how to write GameObject position in the Scene to json file?

on Clicking the button, I m loading the function WriteJsonForLevel(). I have placed three GameObject with the tag name "RedCoin" and I want to write the position of the GameObject to a JSON file. I can get the position of the object, but it's all overwritten. I can only see the last GameObject position (i.e the completion of the loop)
public List<GameObject> levelObjects;
public string level;
public Vector3 pos;
// Start is called before the first frame update
void Start()
{
levelObjects = new List<GameObject>();
}
// Update is called once per frame
void Update()
{
}
public void WritejsonForAll()
{
WriteJsonForLevel();
}
public void WriteJsonForLevel()
{
/* FileStream fs = new FileStream(Application.dataPath + "/sample.json",FileMode.Create);
StreamWriter writer= new StreamWriter(fs);*/
GameObject[] coinObjRed = GameObject.FindGameObjectsWithTag("RedCoin");
putAllObjectInList(coinObjRed);
}
public void putAllObjectInList(GameObject[] p)
{
string path = Application.dataPath + "/text.json";
foreach (GameObject q in p)
{
levelObjects.Add(q);
}
for (int i = 0; i < levelObjects.Count; i++)
{
GameObject lvlObj = levelObjects[i];
Vector3 pos = lvlObj.transform.position;
string posOutput = JsonUtility.ToJson(pos);
File.WriteAllText(path,posOutput);
Debug.Log("position:" + posOutput);
}
}
}
You are using WriteAllText which will overwrite the file every time it is called. As it is overwriting each time it is in the loop, it will only write the last object to the file as every other previous write is overwritten. I would consider making a serialized class of data, assigning the data to it, converting it to a JSON string then saving that.
// stores individual locations for saving
[System.Serializable]
public class IndividualLocation
{
public IndividualLocation(Vector3 pos)
{
xPos = pos.x;
yPos = pos.y;
zPos = pos.z;
}
public float xPos;
public float yPos;
public float zPos;
}
// stores all game locations for saving
[System.Serializable]
public class AllGameLocations
{
public List<IndividualLocation> Locations = new List<IndividualLocation>();
}
public void PutAllObjectInList(in GameObject[] p)
{
string path = Application.dataPath + "/text.json";
// create a new object to write to
AllGameLocations data = new AllGameLocations();
// iterate the objects adding each to our structure
foreach(GameObject obj in p)
{
data.Locations.Add(new IndividualLocation(obj.transform.position));
}
// now that the data is filled, write out to the file
File.WriteAllText(path, JsonUtility.ToJson(AllGameLocations));
}
If you need a snippet on how to load the data properly I can add one.
Edit: Here is a load snippet
public void LoadJSONObject()
{
string path = Application.dataPath + "/text.json";
// if the file path or name does not exist
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Debug.LogWarning("File or path does not exist! " + path);
return
}
// load in the save data as byte array
byte[] jsonDataAsBytes = null;
try
{
jsonDataAsBytes = File.ReadAllBytes(path);
Debug.Log("<color=green>Loaded all data from: </color>" + path);
}
catch (Exception e)
{
Debug.LogWarning("Failed to load data from: " + path);
Debug.LogWarning("Error: " + e.Message);
return;
}
if (jsonDataAsBytes == null)
return;
// convert the byte array to json
string jsonData;
// convert the byte array to json
jsonData = Encoding.ASCII.GetString(jsonDataAsBytes);
// convert to the specified object type
AllGameLocations returnedData;
JsonUtility.FromJsonOverwrite<AllGameLocations>(jsonData, AllGameLocations);
// use returnedData as a normal object now
float firstObjectX = returnedData.Locations[0].xPos;
}
}
Let me know if the Load works, just typed it up untested. Added some error handling as well to assure data exists and the load properly works.

How to use LSTM with a 2d array with DeepLearning4j

i am trying to learn how to use LSTM with deeplearning4j lib.
I created a dummy scenario where i want to get an output (3 classes) based on data that i collected.
I got the data from here (http://www.osservatoriodioropa.it/meteoropa/NOAAMO.TXT) if someone is curious :)
Back to the scenario.
I created 2 matrix, one with features, other with classes that i want to output, just as a test.
When i try the classifier i got
Exception in thread "main" java.lang.IllegalStateException: 3D input expected to RNN layer expected, got 2
i think because the RnnOutputLayer expect a 3d matrix, but i am not able to understand how to populate it. How can i convert a 2d matrix into a 3d matrix correlating the previous event with the new one? The data are a time serie, and i want to relate the classification of the new day based on previous days as well. (I know that probably the data won't fit this scenario and that there are better way to do that, but that's just to learning how to use LSTM, not how to classify this specific dataset)
this is the code so far
public class Test {
public static void main(String args[]) {
int events = 5;
int features = 6;
int classes = 3;
double[][] featureMatrix = new double[events][features];
double[][] labelMatrix = new double[events][classes];
for (int i = 0; i < events; i++) {
for (int f = 0; f < features; f++) {
featureMatrix[i][f] = getFeature(i, f);
}
for (int c = 0; c < classes; c++) {
labelMatrix[i][c] = getResult(i, c);
}
}
INDArray trainingIn = Nd4j.create(featureMatrix);
INDArray trainingOut = Nd4j.create(labelMatrix);
DataSet myData = new DataSet(trainingIn, trainingOut);
MultiLayerNetwork multiLayerNetwork = createModel(features,classes);
multiLayerNetwork.init();
multiLayerNetwork.fit(myData);
}
private static double getFeature(int i, int f) {
//dummy
return 1.;
}
private static double getResult(int i, int c) {
//dummy
return 1.;
}
public static MultiLayerNetwork createModel(int inputNum, int outputNum) {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.trainingWorkspaceMode(ENABLED).inferenceWorkspaceMode(ENABLED)
.seed(123456)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new RmsProp.Builder().learningRate(0.05).rmsDecay(0.002).build())
.l2(0.0005)
.weightInit(WeightInit.XAVIER)
.activation(Activation.TANH)
.list()
.layer(new LSTM.Builder().name("1").nIn(inputNum).nOut(inputNum).build())
.layer(new LSTM.Builder().name("2").nIn(inputNum).nOut(inputNum).build())
.layer(new RnnOutputLayer.Builder().name("output").nIn(inputNum).nOut(outputNum)
.activation(Activation.IDENTITY).lossFunction(LossFunctions.LossFunction.MSE).build())
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
return net;
}
}

Brick Breaker in Unity w json data input

I'm making a brick breaker game in Unity2D, but was told to make the bricks dynamic instead of dragging in cubes from the UI. The idea is to make the brick patterns dynamic, and receive brick data (i.e. positions) from a .json file. I've taken the approach of populating using Instantiate and a prefab, although I'm not sure how to incorporate that with data from .json or if I'm on the right track. Newbie here please help! Even just directing me to concepts would be fantastic.
using System.Collections.Generic;
using UnityEngine;
public class BrickGrid : MonoBehaviour
{
public Transform brickPrefab;
private Vector3[] BrickCoords;
private int totalBricks;
void Start()
{
totalBricks = 4;
BrickCoords = new Vector3[totalBricks];
BrickCoords[0] = new Vector3(-2.2f, 2f, 0f);
BrickCoords[1] = new Vector3(-0.75f, 2f, 0f);
BrickCoords[2] = new Vector3(0.75f, 2f, 0f);
BrickCoords[3] = new Vector3(2.2f, 2f, 0f);
for (int x = 0; x < totalBricks; x++)
{
Instantiate(brickPrefab, BrickCoords[x], Quaternion.identity);
}
}
public static void initGrid(){
}
}
[System.Serializable]
public class BrickCoordinate
{
public Vector3 brickPos;
}
It depends on how your JsonString looks. But something along these lines might be what you need:
Make a class containing a list of blocks.
[System.Serializable]
public class Bricks
{
public List<BrickCoordinate> BrickCoordinates;
}
Then load in your json from wherever you have it, and pass it into Unity's JsonUtility class.
var listOfBlocks = JsonUtility.FromJson<Bricks>(yourJsonStringHere);
And then you can iterate over your list and instantiate each item.
You can view more info here
Edit:
An easy way to get a correct Json string, is to do the exact opposite.
Use the Json function in your code to test and serialize a list of blocks.
(Also i changed the name of the list, because it was misleading)
var test = new Bricks
{
BrickCoordinates = new List<BrickCoordinate>
{
new BrickCoordinate
{
brickpos = new Vector3{ x = 1, y = 2, z = 3};
},
new BrickCoordinate
{
brickpos = new Vector3{ x = 2, y = 2, z = 3};
},
new BrickCoordinate
{
brickpos = new Vector3{ x = 1, y = 4, z = 3};
},
}
};
var jsonString = JsonUtility.ToJson(test);
Debug.Log(jsonString);

Mapping parsed json to java object not working-LibGdx

First time I am using json parsing in my LibGDX project and I am not able to get the parsed results properly.
Here I want to manage the obstacles with properties;name,direction,gap and speed.
I have a json file like this:
"obstacles": [
{"name":"eagle","dir":"up","gap":"100","speed":"0"},
{"name":"bigbird","dir":"up","gap":"200","speed":"0"},
{"name":"elephant","dir":"middle","gap":"300","speed":"0"},
{"name":"tiger","dir":"middle","gap":"400","speed":"0"},
{"name":"bear","dir":"down","gap":"500","speed":"0"},
],
and I am reading the array like this:
public class ReadJson {
static int levelNum = 0;
public ReadJson()
{
}
public static synchronized ArrayList<String> loadLevelJson(int levelno, String name) {
ArrayList obs = new ArrayList<String>();
JsonValue jsonValue = new JsonReader().parse(Gdx.files.internal("levels/level1.json"));
JsonValue objList = jsonValue.get(name);
System.out.println("name:" + objList);
if (name.equals("obstacles")) {
Constants.OBS_COUNT = 0;
for (JsonValue values : objList.iterator()) // iterator() returns a
// list of children
{
obs.add(values.getString("dir"));
Constants.OBS_POSITION[obs.size() - 1] = Integer.parseInt(values.getString("gap"));
Constants.OBS_NAME[obs.size() - 1] = values.getString("name");
Constants.OBS_SPEED[obs.size() - 1] = Integer.parseInt(values.getString("speed"));
Constants.OBS_COUNT++;
}
}
else if (.....)) {
//code
}
return obs;
}
}
Arrays are defined in constant class like this:
public static int OBS_COUNT =20;
public static int[] OBS_POSITION= new int[50];
public static String[] OBS_NAME= new String[50];
public static int[] OBS_SPEED= new int[50];
public static ArrayList<String> obsArray;
I want to map the parsed json to the object bear.For that,I did like this:
private ReadJson readJson;
public ObsObjectFactory() {
readJson = new ReadJson();
readJson.loadLevelJson(1,"obstacles");
Constants.obsArray=readJson.loadLevelJson(1,"obstacles");
}
public Bear createBear() {
Bear bear = new Bear();
bear.setName(Constants.OBS_NAME[Constants.OBS_COUNT]);
bear.setDirection(Constants.obsArray.get(Constants.OBS_COUNT));
bear.setSpeed(Constants.OBS_SPEED[Constants.OBS_COUNT]);
bear.setGap(Constants.OBS_POSITION[Constants.OBS_COUNT]);
bear.setPosition(Constants.EAGLE_X,Constants.EAGLE_Y);
bear.setSize(Constants.BEAR_WIDTH,Constants.BEAR_HEIGHT);
return bear;
}
}
But when I call this createBear()method and run this code,it is throwing IndexOutOfBoundsException in this line:
bear.setDirection(Constants.obsArray.get(Constants.OBS_COUNT));
Parsed values are displaying properly in console.
What mistake I did here?
Your constants don't seem to be constants, as they can change. I found that odd, and as another aside, the code is quite confusing to follow. I'm not entirely sure what you're trying to achieve.
But the index error is likely caused by incrementing your constant (hint, not a constant then, is it?) Constants.OBS_COUNT++; in the readJson method. Given your json file, OBS_COUNT might end up being 5, so Constants.obsArray.get(Constants.OBS_COUNT) is failing as your array only has index positions 0 through 4.

Is there in JSON function which dumps JSON array of dictionary into tab separated text files

I have an JSON array as defined below:-
[
{"Name":"Ayush","Age":24,"Job":"Developer"},
{"Name":"Monika","Age":23,"Job":"Developer"},
{"Name":"Chinmay","Age":23,"Job":"Developer"}
]
I want to dump this into text file in following format:-
Name Age Job
Ayush 24 Developer
Monika 23 Developer
Chinmay 23 Developer
Is there any C# function to accomplish the above? If not, how can i achieve it with minimum memory consumption?
Thanks in advance
There is no such built-in function. You may achieve this by reading JTokens from input stream using JsonTextReader and writing their values into another stream. Stream input and output ensures minimal memory footprint.
using (var inputStream = File.OpenRead("input.json"))
using (var streamReader = new StreamReader(inputStream))
using (var jsonTextReader = new JsonTextReader(streamReader))
using (var outputStream = File.OpenWrite("output.csv"))
using (var streamWriter = new StreamWriter(outputStream))
{
var firstItem = true;
while (jsonTextReader.Read())
{
if (jsonTextReader.TokenType == JsonToken.StartObject)
{
var jObject = JObject.ReadFrom(jsonTextReader);
if (firstItem)
{
streamWriter.WriteLine(string.Join("\t",
jObject.Children().Select(c => (c as JProperty).Name)));
firstItem = false;
}
streamWriter.WriteLine(string.Join("\t",
jObject.Values().Select(t => t.ToString())));
}
}
}
Demo: https://dotnetfiddle.net/2fCRa6. (I used MemoryStream and Console instead of input and output file streams in this demo since .NET Fiddle does not allow file IO, but the idea is the same.)
You can create a class with Name, Age and Job as properties.
public class Info{
public string Name { get; set; }
public int Age { get; set; }
public string Job { get; set; }
}
Then in another function use we can use System.Web.Script.Serialization class(to use this class make sure you have referenced System.Web.Extensions in project references). Once done we can use JavaScriptSerializer class and get list of objects from the json data. Then we can iterate over each item and add it two our file with a tab as a delimeter.
public static void WriteDetailsInFile(string jsonData)
{
var list = new JavaScriptSerializer().Deserialize<List<Info>>(jsonData);
using (var streamWriter = File.AppendText("D:MyFile.txt"))
{
streamWriter.WriteLine("Name\tAge\tJob");
foreach (var item in list)
{
streamWriter.WriteLine(item.Name + "\t" + item.Age + "\t" + item.Job);
}
}
}
//driver
public static void Main()
{
string data = #"[
{ ""Name"":""Ayush"",""Age"":24,""Job"":""Developer""},
{ ""Name"":""Monika"",""Age"":23,""Job"":""Developer""},
{ ""Name"":""Chinmay"",""Age"":23,""Job"":""Developer""}
]";
WriteDetailsInFile(data);
}