gpt4 book ai didi

c - 如何组织从服务器获取的json数据?

转载 作者:行者123 更新时间:2023-11-30 20:29:46 25 4
gpt4 key购买 nike

我正在统一创建一个连接到数据库的应用程序,我需要组织从中获得的信息,我使用以下协程来获取信息:

private IEnumerator GetUsers(string url)
{

using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();

if (webRequest.isNetworkError)
{
Debug.Log(": Error: " + webRequest.error);
}
else
{
Debug.log("DATA:\n" + webRequest.downloadHandler.text);
}
}
}

然后我希望将收到的数据(姓名、代码、密码...等)组织起来打印它们或稍后根据我的需要使用它们,提前非常感谢。

并尝试使用可序列化的类来保存我的数据,但我只使用一个(并且当使用我自己的数据而不是来自服务器的数据时,哈哈),我还尝试制作一个类型为 people 的 vector 来保存多个数据数据,它也不起作用:C 或者更确切地说我不知道​​如何让它工作

[System.Serializable]
public class People
{
public string names, surnames, cedula, password, telephone, address, code,
email;
}

[System.Serializable]
public class PeopleList
{
public People[] users;
}
public class GetSendDate : MonoBehaviour
{
private IEnumerator GetUsers(string url)
{

using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();

if (webRequest.isNetworkError)
{
Debug.Log(": Error: " + webRequest.error);
}
else
{
PeopleList users = new People();

string json = webRequest.downloadHandler.text;

users = JsonUtility.FromJson<PeopleList>(json);

Debug.Log(users.user[0].names);
Debug.Log(users.user[0].surnames);
Debug.Log(users.user[0].cod);

}
}
}
}

这是 webRequest.downloadHandler.text 的响应

[{"userID": 1, "idRole": 1, "email": "ada@gmail.com", "password": "$ 2a $ 10 $ YVXachXCaPBj9vDo.d4itO4vghtCvSMfrmeHCGJqJ6rSneM / hJsPy", "names": "Juanota Rosadita" , "surnames": "sapoton", "cedula": "123", "telefono": "789", "direccion": "147", "code": "258"}, {"idUsuario": 7, " idRole ": 1," email ":" vic@gmail.com "," password ":" $ 2a $ 10 $ JA1rszAgVK52OnWoOWDXneQcVUHwWBi2Di9o2z7kMqrWPyrjGoTnO "," names ":" victor "," Surnames ":" giron "," cedula ":" 1085 "," telefono ":" 313 "," direccion ":" yoquese "," code ":" 217 "}, {" idUsuario ": 8," idRole ": 1," email ":" juanos @ gmail. com "," password ":" $ 2a $ 10 $ 6EAy2e7dXASx2MPDA3vtW.heYuM1wsaEtFmA4Lb6BD0RCTJvm / HSe "," names ":" Juanito "," Surnames ":" Alcachofa "," cedula ":" 789 "," telefono ":" 31358964 " , "address": "123", "code": "753"}]

当我在unity中运行该函数时,出现以下错误:

ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:42) UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30) GetSendDate+d__6.MoveNext () (at Assets/Scripts/GetSendDate.cs:73) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

最佳答案

您可以发送响应webRequest.downloadHandler.text的捕获“Debug.log”吗?

因为您很可能正在尝试转换整个响应,包括 header 。您只需将响应数据转换为 json 即可。

关于c - 如何组织从服务器获取的json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56418765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com