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

时间:2019-06-02 20:34:30

标签: c json sql-server

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

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);
        }
    }
}

然后我希望我收到的数据(名称,姓氏,代码,密码等)可以将其组织起来以打印出来,或者以后根据我的需要使用它们,在此先感谢您。< / p>

并尝试使用一个可序列化的类来保存我的数据,但是我只能使用一个(当我使用自己的数据而不是来自服务器时,大声笑),我还试图制作一个类型为people的矢量来保存多个数据,它也不起作用 :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"}]

统一运行函数时出现以下错误:

  

ArgumentException:JSON必须表示一个对象类型。   UnityEngine.JsonUtility.FromJson(System.String json,System.Type类型)(在C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:42)   UnityEngine.JsonUtility.FromJson [T](System.String json)(在C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:30)   GetSendDate + d__6.MoveNext()(位于Assets / Scripts / GetSendDate.cs:73)   UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator枚举器,System.IntPtr returnValueAddress)(位于C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

2 个答案:

答案 0 :(得分:0)

您能否发送响应webRequest.downloadHandler.text的捕获“ Debug.log”?

因为您很可能尝试转换整个响应,包括标题。您只需要将响应数据转换为json。

答案 1 :(得分:0)

您可以使用Newton Soft Json Utility进行解析。

只需下载DLL文件并将其放在您的Assets / Plugins文件夹中 您可以从以下位置下载: https://www.nuget.org/packages/Unity.Newtonsoft.Json/

using Newtonsoft.Json;

private IEnumerator GetUsers(string url)
    {

 using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {

        yield return webRequest.SendWebRequest();

            if (webRequest.isNetworkError)
            {
                Debug.Log(": Error: " + webRequest.error);
            }
             else if (m_Request.isHttpError)
            {
                Debug.Log(" Server Not Responding ");
            }
            else
            {
                var response = JObject.Parse(m_Request.downloadHandler.text);
                users = JsonConvert.DeserializeObject<PeopleList>(response.SelectToken("data"))
            }
        }
    }
相关问题