json - 无法读取在 Unity 中使用 JSONUtility 创建的 java 脚本( Node JS - Express)中的 JSON 数据

标签 json node.js express unity-game-engine

这是我在 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/

相关文章:

json - 如何检查 ARM 模板变量/参数中的多个条件

c - c中的json数组解析

javascript - 迭代内部数据: [ ] in javascript

node.js - Node/序列 : loading all models

javascript - 在 JavaScript/Node.js 中运行 .map 时如何跳过回调?

javascript - 路由问题,不收集 req.params

node.js - Monk可以直接返回json数据而不是promise吗?

javascript - instanceof 在 JSON.stringify() 中的行为有何不同?

node.js - Mongo降序排序

Javascript - 列表(数组)长度和从列表中获取项目不起作用