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. Dismiss Notice

Why can't I load the JSON with C# correctly?

Discussion in 'Editor & General Support' started by marci8500, Jul 5, 2020.

  1. marci8500

    marci8500

    Joined:
    Jan 13, 2020
    Posts:
    6
    Hello! So I have C# scripts.

    Items.cs
    Code (CSharp):
    1. [System.Serializable]
    2. public class Items {
    3.     public int id { get; set; }
    4.     public string name { get; set; }
    5. }
    6.  
    ItemList.cs
    Code (CSharp):
    1. using System.Collections.Generic;
    2. [System.Serializable]
    3. public class ItemList
    4. {
    5.     public List<Items> Items = new List<Items>();
    6. }
    7.  

    ItemLoader.cs

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class itemLoader : MonoBehaviour
    5. {
    6.     public TextAsset json;
    7.  
    8.     public ItemList ItemList = new ItemList();
    9.  
    10.     void Start()
    11.     {
    12.         Debug.Log("Script started");
    13.         Debug.Log(json.text);
    14.         if (json != null)
    15.         {
    16.             Debug.Log("json variable is not null");
    17.             ItemList = JsonUtility.FromJson<ItemList>(json.text);
    18.             Debug.Log(ItemList.Items.Count);
    19.             foreach (Items item in ItemList.Items)
    20.             {
    21.                 Debug.Log(item.id + " " + item.name);
    22.             }
    23.         } else
    24.         {
    25.             Debug.LogError("Json file is null");
    26.         }
    27.     }
    28. }
    29.  
    30.  

    items.json

    Code (JavaScript):
    1. {
    2.     "items":[
    3.         {
    4.             "id": 0,
    5.             "name": "smallCoin"
    6.         },
    7.         {
    8.             "id": 1,
    9.             "name": "Coin"
    10.         },
    11.         {
    12.             "id": 2,
    13.             "name": "bigCoin"
    14.         }
    15.     ]
    16. }
    output.PNG
    outputList.PNG

    So, why don't I have my list with the JSON data? Can someone please help me?
     
  2. Mariusz_H

    Mariusz_H

    Joined:
    May 4, 2018
    Posts:
    18
    If memory serves me right, JSON serializer does not support properties

    Code (CSharp):
    1.     [System.Serializable]
    2.     public class Items {
    3.         public int id { get; set; }
    4.         public string name { get; set; }
    5.     }
    6.    
    7.  
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Correct.
    These have to be turned into fields.
     
  4. marci8500

    marci8500

    Joined:
    Jan 13, 2020
    Posts:
    6
    I turned them into fields but still, nothing changed.
    I tried both:
    Code (CSharp):
    1. public class Items {
    2.     public int id;
    3.     public string name;
    4. }
    Code (CSharp):
    1. public class Items {
    2.     public int id = 0;
    3.     public string name = "";
    4. }
    Any other ideas, please?
     
  5. Mariusz_H

    Mariusz_H

    Joined:
    May 4, 2018
    Posts:
    18
    Case mismatch in ItemList. In code you use "Items" in JSON source "items".
     
  6. marci8500

    marci8500

    Joined:
    Jan 13, 2020
    Posts:
    6
    OMG, I can't thank you enough! I corrected it and now it works!! I was struggling with this issue for a day. :)