【问题标题】:Unity3D, creating a Json post requestUnity3D,创建 Json 发布请求
【发布时间】:2016-11-26 06:33:47
【问题描述】:

我必须以这种格式在 Json 中创建一个发布请求。

{
"request": {
    "application": "APPLICATION_CODE",
    "auth": "API_ACCESS_TOKEN",
    "notifications": [{
        "send_date": "now", // YYYY-MM-DD HH:mm  OR 'now'
        "ignore_user_timezone": true, // or false
        "content": "Hello world!"
    }]
}
}

这是我第一次序列化 Json 字符串,我不知道该怎么做,我尝试了一些不同的方法,但始终无法获得确切的格式。

非常感谢任何形式的帮助。

谢谢!

【问题讨论】:

    标签: json unity3d pushwoosh


    【解决方案1】:

    首先,您不能对 json 文件发表评论,但我想它现在就在那里。

    然后您可以将您的 json 粘贴到像这样的转换器中 http://json2csharp.com/ 你会得到以下结果:

    public class Notification
    {
        public string send_date { get; set; }
        public bool ignore_user_timezone { get; set; }
        public string content { get; set; }
    }
    
    public class Request
    {
        public string application { get; set; }
        public string auth { get; set; }
        public List<Notification> notifications { get; set; }
    }
    
    public class RootObject
    {
        public Request request { get; set; }
    }
    

    现在您需要修复 JsonUtility 所需的一些问题:

    [Serializable]
    public class Notification
    {
        public string send_date;
        public bool ignore_user_timezone;
        public string content;
    }
    [Serializable]
    public class Request
    {
        public string application;
        public string auth;
        public List<Notification> notifications;
    }
    [Serializable]
    public class RootObject
    {
        public Request request;
    }
    

    最后:

    RootObject root = JsonUtility.FromJson<RootObject>(jsonStringFile);
    

    【讨论】:

    • 非常感谢!我会试试看,然后回复你!! :)
    【解决方案2】:

    你也可以像这样使用SimpleJSON;

    string GetRequest () {
        JSONNode root = JSONNode.Parse("{}");
    
        JSONNode request = root ["request"].AsObject;
        request["application"] = "APPLICATION_CODE";
        request["auth"] = "API_ACCESS_TOKEN";
    
        JSONNode notification = request ["notifications"].AsArray;
        notification[0]["send_date"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        notification[0]["ignore_user_timezone"] = "true";
        notification[0]["content"] = "Hello world!";
    
        return root.ToString ();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 2020-12-11
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多