4

I am programming a game of questions and answers by categories in Unity. The categories are obtained through a PHP script that returns a JSON text,* when I use this solution in the UnityEditor it works correctly, but when I install the .apk on my mobile device, deserialization does not work*.

The connection to the mysql database and the PHP scripts work correctly because before I log in and it works fine

string json = [
    {   "id_cat":"1",
        "nombre_cat":"DAM",
        "id_cat_padre":"0"
    },
    {   "id_cat":"4",
        "nombre_cat":"ASIR",
        "id_cat_padre":"0"
    },
    {   "id_cat":"5",
        "nombre_cat":"DAW",
        "id_cat_padre":"0"
    }
]

then I convert this string to a List of Categories

lsSubCategorias = JsonConvert.DeserializeObject<List<Categoria>>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

I've put traces in the code and it's right on that line where it stops.

I Installed the Newtonsoft.Json using the NuGet and it appears in the references.

I've also dealt with only one category object instead of a list, but it doesn't work either. And it is not a visualization problem because I have created category objects and created buttons with them.

The problem is that it works in Unity Editor but it doesn't work on my android device

I have the following mistake in my mobile:

Type ERROR: System.PlatformNotSupportedException ToString Error(): Error.ToString()

  • Can you include some information on what it exactly is that doesn't work? Just knowing it doesn't work doesn't tell us much, and makes it quite hard for us to help you. Are you getting any errors for example? – Remy Dec 05 '19 at 12:41
  • I thought I had no errors, but now I put the code in a trycatch and I get the following error message: "Operation is not supported on this platform" . This error only appears when I run the game from my mobile, but not from the UnityEditor – Harold Alcalde Solarte Dec 05 '19 at 13:16
  • 1
    any reason why not using unitys json? – BugFinder Dec 05 '19 at 13:24
  • the built in unity json utility is very fast, i would advise using that unless you have a special reason not to – turnipinrut Dec 05 '19 at 15:13
  • 1) If you are using the Xamarin Live Player, then Json.NET is not supported there. See [Newtonsoft.Json deserialize object in Xamarin iOS project](https://stackoverflow.com/q/47379055) and [JsonConvert.SerializeObject always return {} in XamarinForms](https://stackoverflow.com/q/48041823). 2) Can you please [edit] your question to share the full `ToString()` output of the exception including the exception type, message, traceback and inner exception(s) if any? – dbc Dec 05 '19 at 15:38
  • I am using Newtonsoft because it allows you to return a list of objects directly and I thought that with JsonUtility I could not directly return a list of objects. I'm going to try JsonUtility. ps: I have already edited the question – Harold Alcalde Solarte Dec 05 '19 at 16:21

3 Answers3

5

I Installed the Newtonsoft.Json using the NuGet and it appears in the references.

Newtonsoft.Json from NuGet is not supported in Unity3d il2cpp targets, like mobile devices. Use a Newtonsoft.Json fork from the asset store, like this one.

DoctorPangloss
  • 2,994
  • 1
  • 18
  • 22
3

As answered by @DoctorPangloss Newtonsoft.Json has trouble when building with the IL2CPP scripting backend. This is due to Newtonsoft.Json not having any fully AOT-supported builds available.

There are plenty of third-party solutions who delivers Newtonsoft.Json with AOT-support. The linked JSON .NET by ParentElement is a great such solution, however that project has been dead for a couple of years now, and only gives Newtonsoft.Json up to version 8.0.3.

I would suggest looking at my repository, delivering Newtonsoft.Json up to version 12.0.3 (at time of writing) and delivered via the builtin Unity Package Manager: https://github.com/jilleJr/Newtonsoft.Json-for-Unity#readme

To know more, read my post about What even is AOT? from the context of Newtonsoft.Json.

Applejag
  • 1,068
  • 10
  • 17
0

Still same problem with Unity 2021.3.27f1 and Newtonsoft JSON 3.2.1

I solved setting Managed Stripping Level to Minimal in Player Settings / Other Settings / Optimizations.

P.O.W.
  • 1,895
  • 1
  • 16
  • 14