1

I am trying to convert GameInfo.json file to List array by using Json.Utilities. And I want to manipulate this List data.
my items consist of 5 components(date, tournamentname, pos, rounds,total score) here is my Json file(GameInfo.json)

{"Items":
    [
        {
        "date":"11/06/16",
        "tournamentName":"Shriners Hospitals for Children Open",
        "pos":"2",
        "rounds":"62 67 70 67 --",
        "totalScore":"288"  
        },

        {
        "date":"11/06/16",
        "tournamentName":"Shriners Hospitals for Children Open",
        "pos":"2",
        "rounds":"62 67 70 67 --",
        "totalScore":"288"  
        }       
]
}

Game info class include 5 data and and GameInfolist class will be use to make a list structure. GameInfo.json file is converted to GameInfo. After than, Game info is stored as string type(jsonstring) by using the unity.utilities, I try to store my string data into the List in order to manipulate my data. However, i can not reach the foreach loop.( I can't see the Debug.Log message). can you help me to fix my problem. Thank you for reading

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class GameInfoList
{
    public GameInfo gameInfo;
    public string[,] Items = new string[2,5];
    public List<GameInfo> list = new List<GameInfo>();
}

[System.Serializable]
public class GameInfo
{
    public string date;
    public string tournamentName;
    public string pos;
    public string rounds;
    public string totalScore;
}

public class JsonDataConverter : MonoBehaviour
{
    public string path = "GameInfo.json";
    public string jsonstring;
    GameInfoList gameInfoList;
    public string JsonFileToString(string path)
    {
        Debug.Log("test");
        string loadJsonfile = path.Replace(".json", "");
        TextAsset loadedfile = Resources.Load<TextAsset>(loadJsonfile);
        jsonstring = loadedfile.text;
        Debug.Log("jsonstring:" + jsonstring);
        return jsonstring;
    }

    public void Awake()
    {
        Debug.Log("testawake");
        jsonstring = JsonFileToString(path);
        gameInfoList = JsonUtility.FromJson<GameInfoList>(jsonstring);
    }

    public void Start()
    {
        foreach (var object in gameInfoList.list)
        {
            Debug.Log("reach to Inside Loop");
            Debug.Log(object .date);
            Debug.Log(object .pos);
            Debug.Log(object .rounds);
            Debug.Log(object .totalScore);
        }
    } 
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I am not so familiar with u3d, but i think you can debug at the end of Awake() to check if such as gameinfo.list[0].date is right, and debug before foreach block to check if the Start() is executed. – NicoXiang Aug 01 '17 at 05:57
  • i did Debug.Log(gameinfo.list[0].date) but i got this following message – Kyungsik Jeung Aug 01 '17 at 06:10
  • ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[GameInfo].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) JsonDataConverter.Awake () (at Assets/JsonDataConverter.cs:43) – Kyungsik Jeung Aug 01 '17 at 06:10
  • 1
    Your json name "Items" does not even match your class name "GameInfo". Paste that json [here](http://json2csharp.com/) and you will get the correct class. Make sure to remove `{ get; set; }` from it. See the duplicated question for how to read from json. – Programmer Aug 01 '17 at 08:12

1 Answers1

2

'Argument is out of range' means 'gameInfoList' equals null, i think the problem was in this line.

gameInfoList = JsonUtility.FromJson<GameInfoList>(jsonstring);

I think that json string can only convert to object GameInfoList like this:

[System.Serializable]
public class GameInfoList
{
    public List<GameInfo> Items;
}
NicoXiang
  • 429
  • 9
  • 23