0

I'm working on a 3d application for Microsoft Hololens using Unity for rendering. I get Json data from a web service and a local file for some settings. Testing the program doesn't give me any errors and actually works quite fine, but when I try to deploy a version on either an emulator or a real hololens I get the following error.

    Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
JsonReaderException: Error reading JToken from JsonReader. Path '', line 0, position 0.
   at Newtonsoft.Json.Linq.JToken.ReadFrom(JsonReader reader, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JToken.Parse(String json, JsonLoadSettings settings)
   at Assets.Scripts.CreateHyrarchy.Start()
   at Assets.Scripts.CreateHyrarchy.$Invoke0(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method) 
(Filename: <Unknown> Line: 0)

I think it has something to do with the following lines of code.

string data = reader.ReadData(Globals.Globals.RESOURCEFILE);
            JToken jObject = JToken.Parse(data);

Globals gives me my resource path.

public static String RESOURCEFILE = Application.dataPath+"/Resources/pipeLayout.json";

And finaly the json file I would like to read looks like this.

{
  "layers":[
    {
      "type": "water",
      "size-multiplier": 2,
      "color": "#00f",
      "hidden": false
    },
    {
      "type": "gas",
      "size-multiplier": 1,
      "color": "yellow",
      "hidden": false
    },
    {
      "type": "riool",
      "size-multiplier": 5,
      "color": "#FF0000",
      "hidden": false
    }
  ]
}

I'm having the same problem when I call a service, which also return a json string.

they both use a reader object like this.

public String ReadData(String source){
        {
            //Open a call to a webservice
            using (WWW webClient = new WWW(source))
        {
                //Wait until call is completed
                while (!webClient.isDone)
                {
                    new WaitForSeconds(1f);
                }

                //Get the desired data out
                String json = webClient.text.ToString();
                return json;

        }

    }

I try to convert that json data as follows

JObject jObject = JObject.Parse(json);

        //Open features array in the object
        foreach(JObject pipe in jObject["features"].ToArray())
        {
            //Parse JSON Object to Creator
            PipeLineCreator p = pipe.ToObject<PipeLineCreator>();
        }

and Json data looks like this

{
"type": "FeatureCollection",
"totalFeatures": 12,
"features": [
    {
        "type": "Feature",
        "id": "pijpleidingen.3",
        "geometry": {
            "type": "MultiLineString",
            "coordinates": [
                [
                    [
                        4.4361,
                        51.1465
                    ],
                    [
                        4.4356,
                        51.1481
                    ]
                ]
            ]
        },
        "geometry_name": "geom",
        "properties": {
            "type": "gas",
            "diepte_cm": 100,
            "bbox": [
                4.4356,
                51.1465,
                4.4361,
                51.1481
            ]
        }
    },
    {
        "type": "Feature",
        "id": "pijpleidingen.8",
        "geometry": {
            "type": "MultiLineString",
            "coordinates": [
                [
                    [
                        4.4362,
                        51.1474
                    ],
                    [
                        4.436,
                        51.1477
                    ],
                    [
                        4.4361,
                        51.1477
                    ]
                ]
            ]
        },
        "geometry_name": "geom",
        "properties": {
            "type": "gas",
            "diepte_cm": 100,
            "bbox": [
                4.436,
                51.1474,
                4.4362,
                51.1477
            ]
        }
    }
}

Can someone explain me what is happening?

  • This is not how to read file from the Resources folder. You must use `TextAsset` and the `Resources.Load` function. See duplicate for more information. – Programmer Apr 24 '18 at 02:28
  • Reading the file is not the problem. The error gets trown when I try to Parse the data to a JObject. – Quinten.GeoSolutions Apr 24 '18 at 07:34
  • What folder did you put the json? – Programmer Apr 24 '18 at 07:35
  • the local file is in the resources map. The other data I get from a service call using the WWW class in Unity. – Quinten.GeoSolutions Apr 24 '18 at 07:39
  • resources map? What folder? You still haven't answered that. What is the name of the folder in your project that you put the local file? – Programmer Apr 24 '18 at 07:41
  • Resources. Located in the assets of Unity. Path: /Assets//Resources/pipeLayout.json – Quinten.GeoSolutions Apr 24 '18 at 07:43
  • Ok. It is in the Resources folder. Save yourself and I time by actually reading my first comment then the duplicate. **You can't read data in the Resources folder with anything other than the `Resources.Load` API**. Yes, it is true that what you have works in the Editor but you can't do that in standalone build. – Programmer Apr 24 '18 at 07:54
  • This means that `reader.ReadData(Globals.Globals.RESOURCEFILE);` is bad. Hope that's clear to you now. See the section that says **"Text files"** from the duplicate. – Programmer Apr 24 '18 at 07:59
  • I still have that same problem when I use the webservice to get the data. It worked for the local file, many thanks for that. – Quinten.GeoSolutions Apr 24 '18 at 08:10
  • One you load the json or read it from the Web, use Unity's JsonUnility to de-serialize it. See [this](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111?s=1|58.0437#36244111) post for more info. You can't do it, EDIT your post and post what your json looks like and the new code that's not working then I will see what I can do to help – Programmer Apr 24 '18 at 08:17
  • I edited it. I hope you can help me out. – Quinten.GeoSolutions Apr 24 '18 at 08:22
  • You have to use `JsonUtility` instead of `JObject`. See the link I provided in my last comment. You haven't done that yet. – Programmer Apr 24 '18 at 08:27
  • I have read the post and while being useful, it's a bit too simple. I only need certain pices of the data provided and rather not have a lot of Object classes just for conversion. HoloToolkit also uses newtonsoft.json . – Quinten.GeoSolutions Apr 24 '18 at 08:32
  • Since it works for the local file, I read your code again and what you have for `WWW` is wrong. You have to use the WWW class inside a coroutine function and also yield it. You haven't done that, See the [doc](https://docs.unity3d.com/ScriptReference/WWW.html) for an example. After the yielding command, you can now access `webClient.text`. Good luck. – Programmer Apr 24 '18 at 08:43
  • I'll give it a go. Thank you for your support and patience. – Quinten.GeoSolutions Apr 24 '18 at 08:48

0 Answers0