- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我在 Unity 中发出的请求:
UnityWebRequest request = UnityWebRequest.Put(baseUrl + "miniGame1s/", JsonUtility.ToJson(cardsToSend));
“cardsToSend”是下面的对象表单类:
[Serializable]
class Cards
{
public string uid;
public List<Card> cards = new List<Card>();
}
[Serializable]
class Card
{
public int index;
public int theCard;
public bool theAnswer;
public int theState;
public bool playerAnswer;
public float answerMoment;
public float timeAfterAnswer;
public float scoreAfterAnswer;
public int validOrNot;
}
这是服务器中用 Node js 和 Express 编写的代码:
app.put('/api/miniGame1s',(req,res)=>{
console.log(req.body);
const objectSchema = Joi.object().keys({
index: Joi.number().required(),
theCard: Joi.number().required(),
theAnswer: Joi.boolean().required(),
theState: Joi.number().required(),
playerAnswer: Joi.boolean().required(),
answerMoment: Joi.number().required(),
timeAfterAnswer: Joi.number().required(),
scoreAfterAnswer: Joi.number().required(),
validOrNot: Joi.number().integer().required()
});
const arraySchema = {
uid: Joi.string().required(),
cards: Joi.array().items(objectSchema)
};
const result = Joi.validate(req.body, arraySchema);
if(result.error) return res.status(400).send(result.error.details[0].message);
else{
thatGame1InProgressIndex = game1sInProgress.findIndex(g => g.uid == req.body.uid);
console.log('Index: ' + thatGame1InProgressIndex);
for(let i = 0; i < req.body.cards.length;i++)
{
game1sInProgress[thatGame1InProgressIndex].cards[req.body.cards[i].index] = req.body.cards[i];
}
// TODO: validating incomming info and set the validation parameter
// TODO: generate new cards and send them to client.
let newCards = {
uid: req.body.uid,
cards: []
};
for(let i = 0; i < 10 ;i++)
{
let card = CreateACard(game1sInProgress[thatGame1InProgressIndex].cards[game1sInProgress[thatGame1InProgressIndex].cards.length-1].theCard, game1sInProgress[thatGame1InProgressIndex].cards.length);
game1sInProgress[thatGame1InProgressIndex].cards.push(card);
newCards.cards.push(card);
}
res.send(newCards);
}
});
现在,服务器代码还不完整。 (这不是问题)问题是我无法检索服务器中的 JSON 对象。
我尝试了不同的事情,比如统一使用 ToString() 而不是 JSON.ToJson(),或者使用 JSON.parse() 解析服务器中的“req.body”,或者类似的事情。
最好的情况下,我会给出错误:“uid”是必需的这就是 Joi 验证错误。这意味着它无法泄露 Object 的参数。
我尝试使用 POSTMAN 应用程序将具有相同数据的相同请求发送到服务器。它有效。
我认为问题在于 JSONUtility 生成的 JSON 的结构有问题。
我们将不胜感激任何帮助。
最佳答案
首先,我应该说这个链接非常有用:
UnityWebRequest.Post(url, jsonData) sending broken Json
解决方案是您需要将“content-type”设置为“application/json”。这样Express就可以实现JSON对象并完美读取它。为此,请在创建请求之后并将其发送到服务器之前添加此行:
request.SetRequestHeader("Content-Type", "application/json");
关于json - 无法读取在 Unity 中使用 JSONUtility 创建的 java 脚本( Node JS - Express)中的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52356664/
这个问题在这里已经有了答案: Serialize and Deserialize Json and Json Array in Unity (9 个回答) 关闭 5 年前。 这是我用于测试目的的 J
所以我一直在寻找将我的 Json 数组转换为 Unity 中的对象数组的方法。我从一个 2 年前的旧线程中找到了我的解决方案,但没有解释,但我很好奇它是如何工作的。 如果我使用 Visual Stud
这是我的 JSON: {"data":[{"id":1,"layoutLabel":"Sameer Non Custom","orbNumber":"["0","1","2","3"]"}, {"id
我想反序列化包含关卡信息的 json 文件。给定这个名为 1.json 的示例 .json 文件 { "name": "Level One", "map": [ [{ "g
我一直在学习 this 的教程视频。 教程源代码可以在here找到... 我有一个问题,我的 Json 数据都存储在 1 行中, 我希望数据在单个对象数据中换行 这里是 Json 教程的代码 Acto
我有一些数据需要序列化/反序列化,但 JsonUtility 只是没有做它应该做的事情。这是我正在使用的对象: public class SpriteData { public string
这个问题在这里已经有了答案: Serialize and Deserialize Json and Json Array in Unity (9 个回答) 关闭 5 年前。 我在将 json 字符串
我有以下类,它是可序列化的,并且只有字符串作为字段: using UnityEngine; using System.Collections; using System.Collections.Gen
我从 Servlet 获得了一个 JSON 字符串,我想使用 JavaScript 覆盖类型解析这个字符串。 我的整个编码都基于您发布的示例 google我的目标是将 JSON 字符串加载到 Arti
我正在使用 RequestBuilder 读取静态 JSON 文件,并确认我在响应的 .getText() 方法中看到了预期的 JSON。我已经精简了我正在做的事情来尝试找出确切的问题。这就是我能做的
我试图了解如何使用 JSONutil 在 jquery 和 Coldfusion 之间序列化/反序列化 JSON。我无法使用 Coldfusion 7,因此无法在 cfc 中使用 returnform
模型 using System.Collections.Generic; using UnityEngine; [System.Serializable] public class GetPeople
这是我在 Unity 中发出的请求: UnityWebRequest request = UnityWebRequest.Put(baseUrl + "miniGame1s/", JsonUtilit
我是一名优秀的程序员,十分优秀!