Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

JSON multiple structs reading

Discussion in 'Scripting' started by berksonmez39, Jun 26, 2022.

  1. berksonmez39

    berksonmez39

    Joined:
    Apr 19, 2022
    Posts:
    3
    Hello, I' am trying to reach specific question parameters whenever I want. But I am getting an error even though I made the connection from Unity.




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class JSONGReader : MonoBehaviour
    7. {
    8.     string jsonString;
    9.     public TextAsset jsonTextAsset;
    10.  
    11.     [System.Serializable]
    12.     public struct QuestionObject
    13.     {
    14.         public string categoryType;
    15.         public string type;
    16.         public string difficulty;
    17.         public string question;
    18.         public string correctAnswer;
    19.         public string[] incorrectAnswers;
    20.  
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         ReadData();
    26.     }
    27.  
    28.     private void ReadData()
    29.     {
    30.         jsonString = jsonTextAsset.text;
    31.         QuestionObject[] results = JsonHelper.getJsonArray<QuestionObject>(jsonString);
    32.         foreach (QuestionObject a in results)
    33.         {
    34.             Debug.Log(a.categoryType);
    35.         }
    36.     }
    37. }
    38.  
    39. public class JsonHelper
    40. {
    41.     public static T[] getJsonArray<T>(string json)
    42.     {
    43.         string newJson = "{ \"array\": " + json + "}";
    44.         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
    45.         return wrapper.array;
    46.     }
    47.  
    48.     [System.Serializable]
    49.     private class Wrapper<T>
    50.     {
    51.         public T[] array;
    52.     }
    53. }
    54.  

    Code (CSharp):
    1. {
    2.   "response_code": 0,
    3.   "results": [
    4.     {
    5.       "category": "General Knowledge",
    6.       "type": "multiple",
    7.       "difficulty": "medium",
    8.       "question": "What name represents the letter &quot;M&quot; in the NATO phonetic alphabet?",
    9.       "correct_answer": "Mike",
    10.       "incorrect_answers": [ "Matthew", "Mark", "Max" ]
    11.     },
    12.     {
    13.       "category": "General Knowledge",
    14.       "type": "multiple",
    15.       "difficulty": "medium",
    16.       "question": "Which Italian automobile manufacturer gained majority control of U.S. automobile manufacturer Chrysler in 2011?",
    17.       "correct_answer": "Fiat",
    18.       "incorrect_answers": [ "Maserati", "Alfa Romeo", "Ferrari" ]
    19.     },
    20.     {
    21.       "category": "General Knowledge",
    22.       "type": "multiple",
    23.       "difficulty": "medium",
    24.       "question": "What is the name of the popular animatronic singing fish prop, singing such hits such as &quot;Don&#039;t Worry, Be Happy&quot;?",
    25.       "correct_answer": "Big Mouth Billy Bass",
    26.       "incorrect_answers": [ "Big Billy Bass", "Singing Fish", "Sardeen" ]
    27.     },
    28.     {
    29.       "category": "General Knowledge",
    30.       "type": "multiple",
    31.       "difficulty": "medium",
    32.       "question": "What is the last letter of the Greek alphabet?",
    33.       "correct_answer": "Omega",
    34.       "incorrect_answers": [ "Mu", "Epsilon", "Kappa" ]
    35.     },
    36.     {
    37.       "category": "General Knowledge",
    38.       "type": "multiple",
    39.       "difficulty": "medium",
    40.       "question": "Which iconic Disneyland attraction was closed in 2017 to be remodeled as a &quot;Guardians of the Galaxy&quot; themed ride?",
    41.       "correct_answer": "Twilight Zone Tower of Terror",
    42.       "incorrect_answers": [ "The Haunted Mansion", "Pirates of the Caribbean", "Peter Pan&#039;s Flight" ]
    43.     },
    44.     {
    45.       "category": "General Knowledge",
    46.       "type": "multiple",
    47.       "difficulty": "medium",
    48.       "question": "What is real haggis made of?",
    49.       "correct_answer": "Sheep&#039;s Heart, Liver and Lungs",
    50.       "incorrect_answers": [ "Sheep&#039;s Heart, Kidneys and Lungs", "Sheep&#039;s Liver, Kidneys and Eyes", "Whole Sheep" ]
    51.     },
    52.     {
    53.       "category": "General Knowledge",
    54.       "type": "multiple",
    55.       "difficulty": "medium",
    56.       "question": "Where did the pineapple plant originate?",
    57.       "correct_answer": "South America",
    58.       "incorrect_answers": [ "Hawaii", "Europe", "Asia" ]
    59.     },
    60.     {
    61.       "category": "General Knowledge",
    62.       "type": "multiple",
    63.       "difficulty": "medium",
    64.       "question": "Directly between the Washington Monument and the Reflecting Pool is a memorial to which war?",
    65.       "correct_answer": "World War II",
    66.       "incorrect_answers": [ "Vietnam War", "American Civil War", "American Revolutionary War" ]
    67.     },
    68.     {
    69.       "category": "General Knowledge",
    70.       "type": "multiple",
    71.       "difficulty": "medium",
    72.       "question": "The Mexican Beer &quot;Corona&quot; is what type of beer?",
    73.       "correct_answer": "Pale Lager",
    74.       "incorrect_answers": [ "India Pale Ale", "Pilfsner", "Baltic Porter" ]
    75.     },
    76.     {
    77.       "category": "General Knowledge",
    78.       "type": "multiple",
    79.       "difficulty": "medium",
    80.       "question": "What are the three starter Pokemon available in Pokemon Black and White?",
    81.       "correct_answer": "Snivy, Tepig, Oshawott",
    82.       "incorrect_answers": [ "Snivy, Fennekin, Froakie", "Chespin, Tepig, Froakie", "Chespin, Fennekin, Oshawott" ]
    83.     }
    84.   ]
    85. }
     
    Last edited: Jun 26, 2022
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    192
    I guess line 44 does not set the array in wrapper like you expect it to.

    There are sites on the internet where you can test your json.
    Please test the string produced by
    Code (CSharp):
    1. "{ \"array\": " + json + "}";
     
  3. berksonmez39

    berksonmez39

    Joined:
    Apr 19, 2022
    Posts:
    3
    Like you said problem was that. Thank you. With foreach I can call the information of every question. But how can I call for example 5th question's correct answer? Still couldn't figure that out.

    The problem is when I call any category, everything is ok. But when I try to call correctAnswer result is Null.
     
  4. berksonmez39

    berksonmez39

    Joined:
    Apr 19, 2022
    Posts:
    3
    So I found out that the variable name at JSON has to be the same as at the class. Sorry I am new to JSON stuff :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,949
    You may wish to consider using a "real" JSON instead of the built-in JSON.

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
    berksonmez39 likes this.