- Home /
creating json for waypoints and links to them
I want to create json data for saving waypoints positions and their links .
Current Project
using Newtonsoft.Json;
using System.Collections.Generic;
[System.Serializable]
public class Point
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
[System.Serializable]
public class Link
{
[JsonProperty("$ref")]
public string Ref { get; set; }
public Point Point { get; set; } = new Point();
public List<RoadLink> RoadLinks { get; set; } = new List<RoadLink>();
public int? LineCount { get; set; }
public double? SpeedLimit { get; set; }
[JsonProperty("$id")]
public string Id { get; set; }
}
[System.Serializable]
public class RoadLink
{
public Link Link { get; set; } = new Link();
public double SpacerLineWidth { get; set; }
}
[System.Serializable]
public class Content
{
public Point Point { get; set; } = new Point();
public List<RoadLink> RoadLinks { get; set; } = new List<RoadLink>();
public int LineCount { get; set; }
public double SpeedLimit { get; set; }
[JsonProperty("$id")]
public string Id { get; set; }
[JsonProperty("$ref")]
public string Ref { get; set; }
}
[System.Serializable]
public class DataFile
{
[JsonProperty("$content")]
public List<Content> Content { get; set; } = new List<Content>();
[JsonProperty("$type")]
public string Type { get; set; }
}
2nd Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEditor;
using System.IO;
public class JsonKeyWords : MonoBehaviour
{
public Transform[] wayPosition;
List<Vector3> wayPos = new List<Vector3>();
public string typeValue;
//public s
void AssignPositions()
{
for (int i = 0; i < wayPosition.Length; i++)
{
wayPos.Add(new Vector3(wayPosition[i].position.x, wayPosition[i].position.y, wayPosition[i].position.z));
//wayPos[i].x = wayPosition[i].position.x;
//wayPos[i].y = wayPosition[i].position.y;
//wayPos[i].z = wayPosition[i].position.z;
}
}
void main()
{
DataFile mainSource = new DataFile();
mainSource.Content.Add(new Content()
{
Point = new Point { x = wayPos[0].x, y = wayPos[0].y, z = wayPos[0].z },
RoadLinks = new List<RoadLink>(),
LineCount = 1,
Id = "1",
Ref = "0",
SpeedLimit = 3.13
});
mainSource.Type = typeValue;
var jsonString = JsonConvert.SerializeObject(mainSource);
var newPklayer = JsonConvert.DeserializeObject(jsonString);
File.WriteAllText(Application.dataPath + "/test.json", jsonString);
Debug.Log(newPklayer);
}
private void Awake()
{
AssignPositions();
main();
}
}
The Data I want : Json File of a project that i am learning from which do not have reference on how he made this but have scripts to desterilize and use this data
{
"$content": [
{
"Point": {
"x": 644.6,
"y": 11.2,
"z": -827.37
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 595.9198,
"y": 11.06416,
"z": -827.0071
},
"RoadLinks": [
{
"Link": {
"$ref": "0"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 547.2396,
"y": 10.92832,
"z": -826.6442
},
"RoadLinks": [
{
"Link": {
"$ref": "2"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 498.5594,
"y": 10.79249,
"z": -826.2813
},
"RoadLinks": [
{
"Link": {
"$ref": "5"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 473.6187,
"y": 10.79246,
"z": -840.8074
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 446.4278,
"y": 6.237495,
"z": -854.9285
},
"RoadLinks": [
{
"Link": {
"$ref": "11"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 419.2369,
"y": 1.682533,
"z": -869.0497
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 410.5,
"y": 1.682472,
"z": -893
},
"RoadLinks": [
{
"Link": {
"$ref": "8"
},
"SpacerLineWidth": 0
},
{
"Link": {
"$ref": "16"
},
"SpacerLineWidth": 0
}
],
"LineCount": 1,
"SpeedLimit": 3.40282346638529e+38,
"$id": "18"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 409.2,
"y": 1.682472,
"z": -907.2071
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 383.9,
"y": 1.682533,
"z": -907.5
},
"RoadLinks": [
{
"Link": {
"Point": {
"x": 382.6,
"y": 1.682533,
"z": -923
},
"RoadLinks": [
{
"Link": {
"$ref": "11"
},
"SpacerLineWidth": 0
},
{
"Link": {
"$ref": "24"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 379.7,
"y": 1.682503,
"z": -935.3
},
"RoadLinks": [
{
"Link": {
"$ref": "26"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 376.9,
"y": 1.682503,
"z": -942.5
},
"RoadLinks": [
{
"Link": {
"$ref": "30"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 371.1,
"y": 1.682503,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "33"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 332.1663,
"y": 1.682513,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "36"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 293.2326,
"y": 1.682523,
"z": -944.7
},
"RoadLinks": [
{
"Link": {
"$ref": "39"
},
"SpacerLineWidth": 0
},
{
"Link": {
"Point": {
"x": 254.299,
"y": 1.682533,
"z": -944.7
},
{
"$ref": "222"
},
{
"$ref": "224"
},
{
"$ref": "245"
},
{
"$ref": "251"
},
{
"$ref": "258"
},
{
"$ref": "261"
},
{
"$ref": "263"
},
{
"$ref": "265"
},
{
"$ref": "269"
},
{
"$ref": "272"
},
{
"$ref": "278"
},
{
"$ref": "280"
},
{
"$ref": "282"
},
{
"$ref": "284"
},
{
"$ref": "289"
},
{
"$ref": "291"
},
{
"$ref": "314"
},
{
"$ref": "316"
},
{
"$ref": "318"
},
{
"$ref": "320"
}
],
"$type": "Game.Traffic.RoadPoint[]"
}
Now To the main Question,
i can't understand how he is using $ref because they are not in order i can't understand how to create ref to refer like it is created in json file. i am creating a list of RoadLink in which its refers to Link class and then Link refers to RoadLink again, how can i use this ? is it linklist ? any guidence will be appreciated. thank u.
I attempted to start working with your provided JSON in order to possibly give an answer but unfortunately, it's not validating:
Error: Parse error on line 204: ... {
-----------------------^ Expecting 'STRING', got '{'
its beacuase json file is around more then 100,000 characters long and this platform have the limit of 30,000. that json file is not the complete file . link text
this is the complete file