Int64 在传递给带有 SimpleJSON 的函数时更改值
Int64 changes value when passed into a function with SimpleJSON
我从 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();
SimpleJSON 将所有简单标量值(例如布尔值、数字和字符串)存储为字符串。它提供访问器属性和运算符,允许您将值提取为多种不同类型。
例如:
bool b1 = node.AsBool;
bool b1 = node; // Calls operator bool which in turn calls AsBool
这意味着在大多数情况下,您可以简单地使用该节点,就好像它已经是正确的类型一样。
但是没有自动转换为 Int64。如果您尝试使用预期为 int64 的节点,则最佳匹配将是 operator int
,它不会执行您想要的操作。
解决方法是将其作为字符串传递,并使用Int64.Parse
或Int64.TryParse
将其转换为正确的类型。
我从 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();
SimpleJSON 将所有简单标量值(例如布尔值、数字和字符串)存储为字符串。它提供访问器属性和运算符,允许您将值提取为多种不同类型。
例如:
bool b1 = node.AsBool;
bool b1 = node; // Calls operator bool which in turn calls AsBool
这意味着在大多数情况下,您可以简单地使用该节点,就好像它已经是正确的类型一样。
但是没有自动转换为 Int64。如果您尝试使用预期为 int64 的节点,则最佳匹配将是 operator int
,它不会执行您想要的操作。
解决方法是将其作为字符串传递,并使用Int64.Parse
或Int64.TryParse
将其转换为正确的类型。