Newtonsoft.Json works in Unity Editor but not on mobile devices
Asked Answered
C

3

6

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()

Circle answered 5/12, 2019 at 11:29 Comment(6)
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?Irregular
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 UnityEditorCircle
any reason why not using unitys json?Oria
the built in unity json utility is very fast, i would advise using that unless you have a special reason not toHovey
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 and JsonConvert.SerializeObject always return {} in XamarinForms. 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?Advocacy
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 questionCircle
P
6

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.

Plurality answered 5/12, 2019 at 22:5 Comment(2)
This is old post but I would recommend this one: github.com/jilleJr/Newtonsoft.Json-for-UnityHillman
Sadly, but specialized Unity Newtonsoft package of latest version 3.2.1 is not working now on IL2CPP, while works on Mono. Repos like github.com/applejag/Newtonsoft.Json-for-Unity is deprecated and saying that "now Unity supports it", but now it actually just don't work :\Seaborne
Y
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.

Yepez answered 19/1, 2020 at 23:7 Comment(0)
U
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.

Unstep answered 23/7, 2023 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.