Int64 在使用 SimpleJSON 传递到函数时会更改值

问题描述 投票:0回答:2

我从 JSON 获取用户 ID (int64),然后将它们传递到函数中以填充用户数据。但是,一旦通过 iEnumerator 函数传递,我得到的 JSON ID 值就会发生变化。知道为什么会发生这种情况吗?

我已经打印了这些值,并且在传递它们之前确定它们是预期的 JSON 值。

我使用

GetTeachersStudentList
获取 ID 并将它们传递给
PopulateStudentGamesAndTutorial
。我用来存储 ID 和数据的字典是在调用
GetTeachersStudentList
之前初始化的。

IEnumerator GetTeachersStudentList()
{
    //get user information from server call 
    Dictionary<string, string> headers = new Dictionary<string,string>();
    headers.Add("Access-Token", PlayerData.accessToken);
    string url = studentURL += "?staffId=" + PlayerData.currentUser.staffId;

    WWW www = new WWW(url, null, headers);

    yield return www;

    StudentListWebResponse = www.text;
    PlayerData.studentList = StudentListWebResponse; 

    //parse json
    JSONArray students = (JSONArray) JSON.Parse(StudentListWebResponse);
    expectedStudentsToPopulate = students.Count;

    //populate each users data 
    for (int i = 0; i < students.Count; i++)
    {
        string userInformation = students[i].ToString();
        JSONObject studentJsonObject = (JSONObject) JSON.Parse(userInformation);
        foreach (var item in studentJsonObject)
        {
            //look for id, then use that id to populate user data
            if (item.Key == "id")
            {
                StartCoroutine(PopulateStudentGamesAndTutorial(item.Value));
            }
        }
    }

    PlayerData.control.Save();
}

IEnumerator PopulateStudentGamesAndTutorial(Int64 id)
{
    //get games with id 
    Dictionary<string, string> headers = new Dictionary<string,string>();
    headers.Add("Access-Token", PlayerData.accessToken);

    string studentGameURL = serverManager.GamesURL(id);
    WWW gamesWWW = new WWW(studentGameURL, null, headers);
    yield return gamesWWW;  
    PlayerData.StudentListWithGames.Add(id, gamesWWW.text);

    //get tutorials with id 
    string tutorialURL = serverManager.TutorialURL(id);
    WWW wwwGetTutorialsCompleted = new WWW(tutorialURL, null, headers);
    yield return wwwGetTutorialsCompleted;
    JSONArray tutorialArray = (JSONArray) JSON.Parse(wwwGetTutorialsCompleted.text);
    List<int> tutorialIDList = new List<int>();
    for (int i = 0; i < tutorialArray.Count; i++)
    {
        tutorialIDList.Add(tutorialArray[i]["id"]); 
    }
    PlayerData.StudentListWithTutorials.Add(id, tutorialIDList);
    PlayerData.control.Save();
c# unity-game-engine long-integer simplejson int64
2个回答
3
投票

SimpleJSON 将所有简单标量值(例如布尔值、数字和字符串)存储为字符串。它提供访问器属性和运算符,允许您将值提取为多种不同类型。

举个例子:

bool b1 = node.AsBool; 
bool b1 = node; // Calls operator bool which in turn calls AsBool

这意味着在大多数情况下,您可以简单地使用该节点,就好像它已经是正确的类型一样。

但是,不会自动转换为 Int64。如果您尝试使用需要 int64 的节点,则最佳匹配将是

operator int
,这不会执行您想要的操作。

解决方案是将其作为字符串传递,并使用

Int64.Parse
Int64.TryParse
将其转换为正确的类型。


0
投票

在尝试通过 JSON 从 Asp.Net C# 后端发送

UInt64
字段到我的 Angular 前端时,我遇到了类似的问题。收到的值四舍五入为最小的 3 位有效数字。

4070661594142490595 => 4070661594142490600

我能够使用设置为“

WriteAsString
”的 System.Text.Json.Serialization.JsonNumberHandling 属性来解决它。

[Serializable]
public class MyStruct
{
    [JsonNumberHandling(JsonNumberHandling.WriteAsString)]
    public UInt64 ID { get; set; } = 0;
    public int Data{ get; set; } = 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.