【发布时间】:2019-09-18 04:27:28
【问题描述】:
我已经进行了一段时间的故障排除,但没有成功为我的统一应用程序获得正确的响应。从客户端到服务器的登录帖子是成功的,只是我期望的响应不是我团结的正确响应。请检查我下面的代码。
这是模型类
using System;
using System.Collections;
using System.Collections.Generic;
namespace Models
{
[Serializable]
public class Post
{
public string identifier;
public string password;
public string jwt;
public string user;
public override string ToString(){
return UnityEngine.JsonUtility.ToJson (this, true);
}
}
}
这是我的发帖请求
using UnityEngine;
using UnityEditor;
using Models;
using Proyecto26;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;
using SimpleJSON;
public class MainScript : MonoBehaviour {
private readonly string basePath = "http://localhost:1337";
private RequestHelper currentRequest;
public InputField username;
public InputField password;
private void LogMessage(string title, string message) {
#if UNITY_EDITOR
EditorUtility.DisplayDialog (title, message, "Ok");
#else
Debug.Log(message);
#endif
public void Post(){
Debug.Log(username.text);
Debug.Log(password.text);
currentRequest = new RequestHelper {
Uri = basePath + "/auth/local",
Body = new Post {
identifier ="gofor+bs@gmail.com",
password ="12345678"
}
};
RestClient.Post<Post>(currentRequest)
.Then(
res => {
EditorUtility.DisplayDialog("JSON", JsonUtility.ToJson(res, true), "Ok");
Debug.Log(JsonUtility.ToJson(res, true));
}
// this.LogMessage("Success", )
)
.Catch(err => this.LogMessage("Error", err.Message));
}
}
这是 POSTMAN 在成功验证/登录后的响应。
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZDgxOWI5ZWVmYWY3ZDE0MjRhMGNlM2UiLCJpZCI6IjVkODE5YjllZWZhZjdkMTQyNGEwY2UzZSIsImlhdCI6MTU2ODc3NTYzMywiZXhwIjoxNTcxMzY3NjMzfQ.-v33TvKW2pLuLU-w596bzXAamC0Wecpfrv3pOPsB_bI",
"user": {
"_id": "5d819b9eefaf7d1424a0ce3e",
"confirmed": true,
"blocked": false,
"email": "gofor+bs@gmail.com",
"username": "user1",
"provider": "local",
"__v": 0,
"id": "5d819b9eefaf7d1424a0ce3e",
"role": {
"_id": "5d8040be9a4f6d25281e8097",
"name": "Authenticated",
"description": "Default role given to authenticated user.",
"type": "authenticated",
"__v": 0
}
}
}
这是我收到的回复
用户对象是我需要的,但这里它是空的。
{
"identifier": "",
"password": "",
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZDgxOWI5ZWVmYWY3ZDE0MjRhMGNlM2UiLCJpZCI6IjVkODE5YjllZWZhZjdkMTQyNGEwY2UzZSIsImlhdCI6MTU2ODc3ODY1MCwiZXhwIjoxNTcxMzcwNjUwfQ.vCC_EwyH5iAUT6y83PuF92F7Xok4cGhdlkSU7Y0kYqE",
"user": ""
}
我应该如何获取“用户”字符串/对象?我还是 C# 和 Unity 的新手,所以我需要一些专家建议来了解如何在这里做。我感谢你们的帮助。提前谢谢你。
【问题讨论】:
-
user不是字符串而是复杂类型 .... -
有没有办法获得那些复杂的类型?
-
Unity 内置了解析 Json 数据的方法:docs.unity3d.com/Manual/JSONSerialization.html