【问题标题】:How to pass complex JSON in Unity while using web api如何在使用 web api 时在 Unity 中传递复杂的 JSON
【发布时间】:2015-05-07 11:22:54
【问题描述】:

在 REST(WEB API) 中使用 C# 定义了方法。

在这里,我尝试使用 wwwform 在统一 C# 脚本中将以下复杂数据作为字符串发布,但我无法发送/发布关注

string  abc = "[{";
 for (int i = 0; i < SessionNeedles.Count; i++)
        {
            abc += "\"PositionX\":\"" + SessionNeedles[i].PositionX + "\",";
            abc += "\"PositionY\":\"" + SessionNeedles[i].PositionY + "\",";
            abc += "\"PositionZ\":\"" + SessionNeedles[i].PositionZ + "\",";
            abc += "\"RotationX\":\"" + SessionNeedles[i].RotationX + "\",";
            abc += "\"RotationY\":\"" + SessionNeedles[i].RotationY + "\",";
            abc += "\"RotationZ\":\"" + SessionNeedles[i].RotationZ + "\",";
            abc += "\"RotationW\":\"" + SessionNeedles[i].RotationW + "\",";
            abc += "\"ScaleX\":\"" + SessionNeedles[i].ScaleX + "\",";
            abc += "\"ScaleY\":\"" + SessionNeedles[i].ScaleY + "\",";
            abc += "\"ScaleZ\":\"" + SessionNeedles[i].ScaleZ + "\",";
            abc += "\"Prefab\":\"" + SessionNeedles[i].Prefab + "\",";
            abc += "\"NeedleName\":\"" + SessionNeedles[i].NeedleName + "\"";
            abc += "},{";
        }
        abc = abc.Substring(0, abc.Length - 2) + "]";
        WWWForm form = new WWWForm();
        form.AddField("Duration", 5);
        form.AddField("PatientId", "7");
        form.AddField("SessionNeedles", abc);
        WWW www = new WWW(_AddSessionUrl, form);
        StartCoroutine("AddSession", www); 

【问题讨论】:

  • 您需要在 IEnumerator 中制作表单和 www 并等待完成。
  • 请举个例子。对我很有帮助。 AddSession 已经是一个 Ienumrator。 IEnumerator AddSession(WWW www) { yield return www; if (www.error == null) { print("Done"); } else { Debug.Log("WWW 错误:" + www.error); } }
  • 你在文档上有这个,检查这个链接:docs.unity3d.com/ScriptReference/WWWForm.html

标签: c# json unity3d


【解决方案1】:
  1. 我不确定您所说的“无法发送”是什么意思。 您在 Unity 端收到错误消息吗?在服务器上?

  2. 请使用 JSON 序列化库(即JSON.NET、SimpleJSON

  3. 你没有附上协程代码,但你必须要么yield return www;

  4. 如果仍然没有错误,请使用数据包嗅探器(即Fiddler / Charles)并查看从您的 Unity 客户端发送的数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多