Use LitJson and Json file served from a URL in place of local Json file

Needing help with a json (LitJson) issue. I am trying to read a json file from the web in the same way as I am able to read a local json file.

the way I read the local file is like this

itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Art_.json"));```

when I debug
```itemData``` I get back ```JsonData array``` in the console.

I am successfully reading the json file from the web with this:

```UnityWebRequest webjson = UnityWebRequest.Get("http://somewebsite.com/Art_.json");```
when I debug ```webjson``` i get back the entire json file in the console.

I am not sure how the json is being parsed differently, and why I can not replace the locally served json data with the json data being served from the url.

Thank you for any and all help.

First, you can use Application.StreamingAssets instead of how you have it setup with Application.dataPath. Looks cleaner.

Next, webjson is a UnityWebRequest and itemData is a JsonData. They are two different types. Since you are casting the json string to a JsonData class, you’re not going to get the full json string when you debug it. If you simply cast the File.ReadAllText into a string variable and debug that string you should see the full json file.

Thank you, I really appreciate the explanation between the data types as I did not know that before.
I suppose to be clear, If I want to use webjson as JsonData, how could I convert a my UnityWebRequest into JsonData?
This is for an inventory

I believe the text from a UnityWebRequest is in the downloadhandler

So try

webjson.downloadHandler.text

Try the JsonMapper.ToObject on that. Essentially you just need to get the json in it’s string form and then serialize it into the class.

See if I’m understanding this scenario correctly. Chance is the request has not completed by the time you try to parse the content. You need to await the response of the web request. Best-practice solution is by using coroutines.

Looking at the example from the docs (Unity - Scripting API: Networking.UnityWebRequest.Get) then you would like to do something of the similar:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using LitJson;

public class Example : MonoBehaviour
{
    public string artJsonUrl = "http://somewebsite.com/Art_.json";
    public JsonData itemData;
   
    void Start()
    {
        StartCoroutine(FetchArtJsonData());
    }

    IEnumerator FetchArtJsonData()
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(artJsonUrl))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            if (webRequest.isNetworkError)
            {
                Debug.LogError($"Failed to fetch art data due to network fault.\nError: {webRequest.error}");
            }
            else if (webRequest.isHttpError)
            {
                Debug.LogError($"Failed to fetch art data, response code {webRequest.responseCode}.\nResponse:\n{webRequest.downloadHandler.text}");
            }
            else
            {
                itemData = JsonMapper.ToObject(webRequest.downloadHandler.text);
                Debug.Log("Successfully fetched art data.");
            }
        }
    }
}