- Home /
Generate Json from Class Type List
Hi, I want to create a json string from a structure of classes. The point is that the result is not that what i want.
The result has to be like that;
{"vfbs":[{"url":"ButtonInputPrefab","pattern":"test-pattern","orientation":{"x":0.0,"y":0.0,"z":0.0},"size":{"x":0.0,"y":0.0},"position":{"x":1024.0,"y":786.0,"z":0.0}}]}
and what I get is this:
{"vfbs":[{"url":"ButtonInputPrefab","pattern":"test-pattern","orientation":{"x":0.0,"y":0.0,"z":0.0},"size":{"x":0.0,"y":0.0},"position":{"x":0.0,"y":0.0,"z":0.0}}]}
The differences are in the position area or the other classes inside of the classes.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System;
using System.Collections.Generic;
public class Test : MonoBehaviour
{
public GameObject textInput;
public string gameState;
public MainObjectData mainObject;
List<InnerObjectData> objectList = new List<InnerObjectData>();
void GetJson()
{
objectList.Add(createSubObject(this.name, "test-origin", 0.0f, 0.0f, 0.0f, 1024.0f, 768.0f, 0.0f, 0.0f, 0.0f));
mainObject.vfbs = objectList.ToArray();
string test = objectList.ToString();
Debug.Log("Test = " + test);
string generatedJsonString = JsonUtility.ToJson(mainObject);
Debug.Log("generatedJsonString: " + generatedJsonString);
}
public InnerObjectData createSubObject(string url, string origin, float orientationx, float orientationy, float orientationz, float sizex, float sizey, float positionx, float positiony, float positionz)
{
Debug.Log( url + origin + orientationx + orientationy + orientationz + sizex + sizey + positionx + positiony + positionz);
InnerObjectData myInnerObject = new InnerObjectData();
Orientation OrientationObject = new Orientation();
Size SizeObject = new Size();
Position PositionObject = new Position();
myInnerObject.url = url;
myInnerObject.origin = origin;
OrientationObject.x = orientationx;
OrientationObject.y = orientationy;
OrientationObject.z = orientationz;
SizeObject.x = sizex;
SizeObject.y = sizey;
PositionObject.x = positionx;
PositionObject.y = positiony;
PositionObject.z = positionz;
return myInnerObject;
}
}
[System.Serializable]
public class MainObjectData
{
public InnerObjectData[] vfbs;
}
[System.Serializable]
public class InnerObjectData
{
public string url;
public string origin;
public Orientation orientation;
public Size size;
public Position position;
}
[System.Serializable]
public class Orientation
{
public float x;
public float y;
public float z;
}
[System.Serializable]
public class Size
{
public float x;
public float y;
}
[System.Serializable]
public class Position
{
public float x;
public float y;
public float z;
}
Can somebody help me there?
Thanks Pad
Answer by flashframe · Sep 22, 2016 at 06:41 PM
You're creating a "orientation", "size" and "position" object, but you aren't assigning them to the InnerObject.
public InnerObjectData createSubObject(string url, string origin, float orientationx, float orientationy, float orientationz, float sizex, float sizey, float positionx, float positiony, float positionz)
{
InnerObjectData myInnerObject = new InnerObjectData();
Orientation orientationObject = new Orientation();
Size sizeObject = new Size();
Position positionObject = new Position();
myInnerObject.url = url;
myInnerObject.origin = origin;
orientationObject.x = orientationx;
orientationObject.y = orientationy;
orientationObject.z = orientationz;
sizeObject.x = sizex;
sizeObject.y = sizey;
positionObject.x = positionx;
positionObject.y = positiony;
positionObject.z = positionz;
myInnerObject.orientation = orientationObject;
myInnerObject.size = sizeObject;
myInnerObject.position = positionObject;
return myInnerObject;
}
}
Your answer
Follow this Question
Related Questions
How to get object array from json? 0 Answers
Get JSON array object string value 2 Answers
Multiple Cars not working 1 Answer
I want to convert image data(Texture2D) to Base64 string and store in JSON file in Unity3D(C#). 0 Answers
CSharp Classes = Scripts? Difference between private string and normal string? 1 Answer