- Home /
Question by
manuelmangual · Apr 30, 2016 at 11:39 AM ·
c#unity 5json
Issues with JSONUtility
I have this JSON file:
{
"Name": "Corellian Engineering Corporation",
"Title": "CEC",
"Version": "1.0",
"DateCreated": "2016-04-29T14:00:00.00",
"CreatedBy": "Manuel Mangual",
"Path": null,
"LastUpdated": "2016-04-29T16:00:00.00",
"LastUpdatedBy": "Manuel Mangual",
"Settings": [ ],
"Objectives": [ ],
"Scenes": [
{
"Name": "YT1300",
"Title": "YT1300",
"SourceImage": "Assets/Graphics/UI/milleniumFalcon.jpg",
"Description": "YT-1300 light freighters were a type of light freighter that operated in the galaxy during the final days of the Galactic Republic and the reign of the Galactic Empire. Its side-mounted cockpit and front-facing prongs allowed it to push containers in orbital freight yards. The Millennium Falcon, originally known as YT-1300 492727ZED, was a modified YT-1300 light freighter with a storied history stretching back to the decades before the Clone Wars and the rise of the Galactic Empire. Manufactured by the Corellian Engineering Corporation in 60 BBY, the light freighter was first owned by Corell Industries Limited and underwent several name changes before being bought by the smugglers Kal and Dova Brigger in 48 BBY, and the ship eventually ended up as the property of the secretive Republic Group under the name Stellar Envoy by 29 BBY. The Envoy was destroyed in a collision with a bulk freighter above Nar Shaddaa, but it was rebuilt and served various owners under different names before Quip Fargil named it after the bat-falcon, and it eventually fell into the hands of Lando Calrissian after a game of sabacc—but Calrissian himself lost the ship in another game of sabacc to the smuggler Han Solo several years later.",
"Areas": [
{
"Name": "Cockpit",
"Title": "Cockpit"
},
{
"Name": "EngineRoom",
"Title": "Engine Room"
},
{
"Name": "NumberThreeHold",
"Title": "Number Three Hold"
},
{
"Name": "Hyperdrive",
"Title": "Hyperdrive"
},
{
"Name": "Hyperdrive",
"Title": "Hyperdrive"
},
{
"Name": "MainHold",
"Title": "Main Hold"
},
{
"Name": "PassageTubeToCockpit",
"Title": "Passage Tube To Cockpit"
}
]
}
]
}
and what I want is to load its elements into Unity GUI objects I set up in the inspector. I made this class for that purpose:
using UnityEngine;
using System;
using UnityEngine.UI;
public class ShipDataReader: MonoBehaviour {
void Start()
{
Scenes _scenes = new Scenes();
string json = JsonUtility.ToJson(_scenes);
_scenes = JsonUtility.FromJson<Scenes>(json);
}
}
[Serializable]
public struct Scenes
{
public int index;
public string title;
public string description;
public Image sourceImage;
public string[] shipSection;
}
but the problem is that it doesn't read the json file I created. Can anyone help me structure this class so it can read from the correct json file? And also, how would I be able to access each element in the json file? Thanks in advance!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612075859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity json get data 2 Answers
Null Reference exception when loading json data to dictionary 1 Answer
Can't read float array into C# from JSON using Simple JSON 2 Answers
Multiple Cars not working 1 Answer