Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

JSON parse error: The document root must not follow by other values

Writer Matthew Martinez

I stumbled upon this issue but i couldnt find a soultion for it. There is a post on StackOverflow with this Problem but the author created the solution himselfe and didnt share it so.

I have Json files which need to be read and then put into a List so i can Instatiate objects with these Datapoints. But I keep getting this Error. Would be very nice if you could help

using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
[Serializable]
public class InitialOrbit
{ public string type; public Attributes attributes; public Relationships relationships;
}
[Serializable]
public class Attributes
{ public string epoch; public string mAno; public string inc; public string frame; public string raan; public string sma; public string ecc; public string aPer;
}
[Serializable]
public class Relationships
{ public OrbitObject orbitObject; public int id; public Links links;
}
[Serializable]
public class OrbitObject
{ public Links links;
}
[Serializable]
public class Links
{ public string self; public string related;
}
public class GetDepris : MonoBehaviour
{ public string jsonLibraryPath = "myPath"; List<InitialOrbit> orbits; public void ReadOrbitFiles() { string path = jsonLibraryPath + "Document.json"; Debug.Log(path); if (File.Exists(path)) { string json = File.ReadAllText(path); Debug.Log("json: " + json); InitialOrbit initialOrbit = JsonUtility.FromJson<InitialOrbit>(json); orbits.Add(initialOrbit); } } void Start() { ReadOrbitFiles(); }
}

A Json File looks like this:

{"data":
[{"type": "initialOrbit", "attributes": {"epoch": "1965-09-14", "mAno": null, "inc": 63.44, "frame": "J2000", "raan": null, "sma": 6608000.0, "ecc": 0.011, "aPer": 64.0}, "relationships": {"object": {"links": {"self": "/api/initial-orbits/1/relationships/object", "related": "/api/initial-orbits/1/object"}}}, "id": "1", "links": {"self": "/api/initial-orbits/1"}},
{"type": "initialOrbit", "attributes": {"epoch": "1965-05-27", "mAno": null, "inc": 51.81, "frame": "J2000", "raan": null, "sma": 6651000.0, "ecc": 0.011, "aPer": 40.0}, "relationships": {"object": {"links": {"self": "/api/initial-orbits/4/relationships/object", "related": "/api/initial-orbits/4/object"}}}, "id": "4", "links": {"self": "/api/initial-orbits/4"}},
{"type": "initialOrbit", "attributes": {"epoch": "1965-05-30", "mAno": null, "inc": 51.82, "frame": "J2000", "raan": null, "sma": 6621000.0, "ecc": 0.008, "aPer": 39.0}, "relationships": {"object": {"links": {"self": "/api/initial-orbits/5/relationships/object", "related": "/api/initial-orbits/5/object"}}}, "id": "5", "links": {"self": "/api/initial-orbits/5"}},
{"type": "initialOrbit", "attributes": {"epoch": "1965-05-29", "mAno": null, "inc": 95.78, "frame": "J2000", "raan": null, "sma": 6586000.0, "ecc": 0.009, "aPer": 134.0}, "relationships": {"object": {"links": {"self": "/api/initial-orbits/6/relationships/object", "related": "/api/initial-orbits/6/object"}}}, "id": "6", "links": {"self": "/api/initial-orbits/6"}},
...

It repeads the data with other values 100 times in 1 file

4

1 Answer

you have to create one more class, and fix another, also install Newtonsoft.Json for Unity

Root root = JsonUtility.FromJson<Root>(json);

classes

using Newtonsoft.Json;
public class Root
{ public List<Data> data { get; set;}
}
public class Data
{ public string type { get; set; } public Attributes attributes { get; set; } public Relationships relationships { get; set; } public string id { get; set; } public Links links { get; set; }
}
public class OrbitObject
{ public Links links { get; set; }
}
public class Relationships
{ [JsonProperty("object")] public OrbitObject orbitObject;
}
public class Attributes
{ public string epoch { get; set; } public object mAno { get; set; } public double inc { get; set; } public string frame { get; set; } public object raan { get; set; } public double sma { get; set; } public double ecc { get; set; } public double aPer { get; set; }
}
public class Links
{ public string self { get; set; } public string related { get; set; }
}

if you want just an InitalOrbit you can get it

InitialOrbit initialOrbit = root.data[0];
//or
InitialOrbit initialOrbit = JsonUtility.FromJson<Root>(json).data[0];

And I highly recommend you do install Newtonsoft.Json for Unity and use it instead of JsonUtility

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.