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

Loop through Json and add result in array?

Discussion in 'Scripting' started by EwnT9, May 1, 2017.

  1. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    Hi,
    I would like to load multiple lines like character statistics, they all have "name" and "power" for example. I don't know how to loop through the result in comment. So I tried with this link, it talks about a List, but here the .Count of the list gives 0. Would you know how to solve one or the other way? Thanks

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Collections.Generic;
    5.  
    6. [System.Serializable]
    7. public class PlayerStats{
    8.  
    9.     public int life;
    10.     public string name;
    11.     public int energy;
    12. }
    13.  
    14. [System.Serializable]
    15. public class PlayerStatsList {
    16.  
    17.     public List <PlayerStats> playerStats;
    18. }
    19.  
    20. public class Sauvegarde : MonoBehaviour
    21. {
    22.  
    23.     public PlayerStatsList myPlayerStatsList = new PlayerStatsList();
    24.  
    25.     void Start () {
    26.         string jsonText = File.ReadAllText(Application.dataPath + "/Scripts/characters.json");
    27.         JsonUtility.FromJsonOverwrite(jsonText, myPlayerStatsList);
    28.         Debug.Log("jsonText : "+jsonText);//works
    29.         Debug.Log("count : "+myPlayerStatsList.playerStats.Count);// = 0
    30.         foreach (PlayerStats p in myPlayerStatsList.playerStats){
    31.             Debug.Log("life : "+p.life);
    32.         }
    33.         //Person[] p = CreateFromJSON(mj); //error : Cannot convert Person to Person[]....
    34.     }
    35.     /*
    36.     public Person[] CreateFromJSON(string jsonString){
    37.         return JsonUtility.FromJson<Person[]>(jsonString);
    38.     }
    39.     */
    40. }
    The json is like that :

    Code (CSharp):
    1. {
    2.     "PlayerStats": [
    3.         {
    4.             "life": "10",
    5.             "name": "Kratos",
    6.             "energy": 100
    7.         },
    8.         {
    9.             "life": "80",
    10.             "name": "Nathan",
    11.             "energy": 20
    12.         },
    13.         {
    14.             "life": "90",
    15.             "name": "Link",
    16.             "energy": 25
    17.         }
    18.     ]
    19. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    EwnT9 likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    Did you build the json by hand?

    The json doesn't seem like it would match your classes. "PlayerStats" should for one be "playerStats", as it's supposed to match the variable name, not the class name.
     
    animatekoi, Brathnann and EwnT9 like this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Also, "life" in your json is a string (surrounded by quotes), but is an int in your code.
     
    Brathnann, EwnT9 and Baste like this.
  5. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    Guys thank you very much. It was indeed mistakes in the code.
     
  6. ButlerJ

    ButlerJ

    Joined:
    Nov 24, 2016
    Posts:
    1
    Can you post the fixed code?

    Many thanks
     
  7. EwnT9

    EwnT9

    Joined:
    Apr 20, 2017
    Posts:
    31
    Sorry I did not keep the code, I erased the project at the time.