JsonUtility.ToJson return wrong content

hey, when i use unity JsonUtility.ToJson to make my object to string,but on some mobile device, return wrong result like:
{“hovered”:[{“m_FileID”:-696942,“m_PathID”:0},{“m_FileID”:-696926,“m_PathID”:0},{“m_FileID”:-696850,“m_PathID”:0},{“m_FileID”:-696818,“m_PathID”:0},{“m_FileID”:-696802,“m_PathID”:0},{“m_FileID”:236956,“m_PathID”:0},{“m_FileID”:236926,“m_PathID”:0},{“m_FileID”:236954,“m_PathID”:0}]}

my code is this:

public class Pay
{
    [Serializable]
    public class ActivityExtendData
    {
        public int activityID;
        public int index;
    }

    [Serializable]
    public class ShopExtendData
    {
        public int shopID;
    }
}

// other place call the function
Pay.ShopExtendData extData = new Pay.ShopExtendData();
extData.shopID = 1002;
string extStr = JsonUtility.ToJson(extData);

the return string “extStr” most time return right json string, on some mobile device return wrong string:
{“hovered”:[{“m_FileID”:-696942,“m_PathID”:0},{“m_FileID”:-696926,“m_PathID”:0},{“m_FileID”:-696850,“m_PathID”:0},{“m_FileID”:-696818,“m_PathID”:0},{“m_FileID”:-696802,“m_PathID”:0},{“m_FileID”:236956,“m_PathID”:0},{“m_FileID”:236926,“m_PathID”:0},{“m_FileID”:236954,“m_PathID”:0}]}

My unity version is:5.6.6f2, use il2cpp

Can you indicate, which part of the string you think is wrong?

the right format should be:“{“shopID”:1002}”

There is nothing special about your ShopExtendData class.
Meaning, if other classes are serialized, that you are probably overriding something somewhere.
Or using different class as base.
Too little show, to deduct anything.

I don’t see activityID in string either.

I submit this problem the reason is the code is so simply, but just on some mobile device occurs this problem.
the actvityID, i do not use “ActivityExtendData” this class, it will not contain “activityID” for sure.

can you show us full extData class?

the extData class is full, at this time, i find maybe the stack frame is wrong,look the full function i used:

    private void OnItemClick(LoopBaseNode node, PointerEventData data)
    {
        RrechargeNode rnode = node.GetComponent<RrechargeNode>();


        if (UserInfoManager.instance.GetCurrentTime() < rnode.curInfo.expireTime)
        {
            ScreenManager.instance.ShowTip(GetTextInfo(RechargelangEnum.rechargelang27));
            return;
        }
        //... buy
        m_curRechargeDataInfo = rnode.curInfo;
        string productId = m_curRechargeDataInfo.data.rechargeId.ToString();
        Pay.ShopExtendData extData = new Pay.ShopExtendData();
        extData.shopID = m_curRechargeDataInfo.data.id;
        string extStr = JsonUtility.ToJson(extData);
        T7Game.UnitySDKInterface.U3dSdkInterface.GetPay().StartPay(productId, extStr);
    }

and i find the PonterEventData has member “hovered”:

namespace UnityEngine.EventSystems
{
    //
    // Summary:
    //     Event payload associated with pointer (mouse / touch) events.
    public class PointerEventData : BaseEventData
    {
        //
        // Summary:
        //     List of objects in the hover stack.
        public List<GameObject> hovered;
     }
}

and the member “hovered” is a gameobject list
thank you very much talk with me.@Antypodish

I can not see anything obvious here unfortunately, which could lead to an issue.

Are you positive, you have only one OnItemClick method. What I am trying to confirm, if you maybe have other version of OnItemClick method, without shopID.

I doubt this part of the string just disappear for no reason on some mobile devices.
But if you can reproduce every time, then you could potentially fail file the bug. If that is indeed the case.

Or maybe other thought, there was older file version, without that ID string, and can not be overwritten? Just thoughts.

it always work correctly, but there are some device call this function will occur this problem, I know this because our server receive this wrong json string, so i just replace this statement “extStr = JsonUtility.ToJson(extData);” with string.format, our server never receive this wrong json. maybe the only problem is i used “nested class”? maybe just Unity guys can answer this question.

you know we use lua can replace this function, that called hotfix, so i can not understand why this happened?

So best you can do now, if you got data, collect which devices are troubling and if you can replicate conditions…

ok, thank you very much again, @Antypodish

ok, I have find out that just my code bugs, I am sorry of wasting yours time!

1 Like