0

I want to get my JSON file in server then deserialize it to the gameobject... So, I'm sure I've to use WWW class...

This is my script

IEnumerator LoadPertanyaanData() {

    WWW wwwDataFilePath = new WWW (dataFilePath);
    yield return wwwDataFilePath;

    string dataAsJSON = wwwDataFilePath.text;
    DataController loadedData = JsonUtility.FromJson<DataController> (dataAsJSON);
    allKategori = loadedData.allKategori;

}

And I have some error :

ArgumentException: Cannot deserialize JSON to new instances of type 'DataController.' UnityEngine.JsonUtility.FromJson[DataController] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:24) DataController+c__Iterator0.MoveNext () (at Assets/Scripts/DataController.cs:35) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

Shiro
  • 25
  • 2
  • 8

1 Answers1

1

Your problem seems to be with this line:

string dataAsJSON = File.ReadAllText(wwwDataFilePath.text);

The File.ReadAllText method takes a local filename or path. It will throw an ArgumentException if its input contains certain characters that are invalid in local file path names.

You seem to be getting this exception because, as per your comment, the input string is http://localhost/game/data.json, which contains the invalid character :.

The text property on the WWW object is all you need to get a string from the remote URL:

string dataAsJSON = wwwDataFilePath.text;
Vassalware
  • 30
  • 3
  • 8
  • Ow yeah of course... Thanks... :) But then I've another error, 'ArgumentException : Cannot deserialize JSON to new instances of type DataController'... – Shiro Apr 12 '17 at 07:56
  • @Shiro About how to serialize and deserialize json, have a look at [this](http://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111#36244111). – zwcloud Apr 12 '17 at 07:59
  • @Shiro Is 'DataController' a `MonoBehaviour`? JsonUtility can only be used for classes that aren't `MonoBehaviour`s, and are marked with the `[System.Serializable]` attribute. – Vassalware Apr 12 '17 at 08:00