0
\$\begingroup\$

Greetings!

I'm working on a class that loads and parses .json files and returns information from the loaded .json, but I have problems reading the nested arrays of my .json files.

Turning the serialized object public and looking at the Inspector, the 'Choises' array is not being read at all. It contains four objects, each contain a string and an integer correspondingly.

enter image description here

using SimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class JsonParser : MonoBehaviour
{
    private string loadedJson;
    public StoryData storyData;

    [Serializable]
    public struct choice
    {
        public string choiceText;
        public int choicePointer;
    }

    [Serializable]
    public struct storyImage
    {
        public string imagePath;
    }

    [Serializable]
    public struct storyText
    {
        public string text;
    }

    [Serializable]
    public struct placeholderStory
    {
        public string storyText;
        public string storyImage;
        public List<choice> choises;
    }

    [Serializable]
    public struct StoryData
    {
        public placeholderStory[] placeholderStory;
    }

    public void LoadJsonFile(string filename)
    {
        loadedJson = File.ReadAllText(Application.dataPath + filename);
        storyData = JsonUtility.FromJson<StoryData>(loadedJson);
    }

    public string GetChoiceText(int storyPosition, int choiceIndex)
    { 
        string choiceString = storyData.placeholderStory[storyPosition].choises[choiceIndex].choiceText;
        Debug.Log(choiceString);
        return choiceString;
    }

    public string GetStoryText(int storyPosition)
    {
        string storyString = storyData.placeholderStory[storyPosition].storyText;
        return storyString;
    }

    public string GetStoryImage(int storyPosition)
    {
        string imageString = storyData.placeholderStory[storyPosition].storyImage;
        return imageString;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Have you checked your JSON with an online checker? \$\endgroup\$ Commented May 17, 2020 at 12:05
  • \$\begingroup\$ @Engineer I actually built the JSON with an online editor. \$\endgroup\$ Commented May 17, 2020 at 14:51

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.