【问题标题】:asp webservice and json formattingasp webservice和json格式
【发布时间】:2012-05-31 12:46:53
【问题描述】:

我正在尝试在 asp 中创建一个 web 服务,它将成为 jQuery.Gantt 中的用户 http://mbielanczuk.com/jquery-gantt/

此插件支持的格式如下所示:

[{  "name"  : "Task#1"
, "desc"  : " Task Desc"
, "values": [
  {  "from"       : "/Date(1296547200000)/"
   , "to"         : "/Date(1296554400000)/"
   , "desc"       : "<b>Task #1<br>"
   , "customClass": "ganttRed" (optional)
   , "label"      : "Task #1" (optional)
  },
  {  "from"       : "/Date(1296637200000)/"
   , "to"         : "/Date(1296649800000)/"
   , "desc": "<b>Task #</b>"
   , "customClass": "ganttOrange" (optional)
   , "label": "Task #1" (optional)
  }
]
},
...
]

我使用 JSON.NET 创建了一个 Web 服务,如下所示:

[JsonObject(MemberSerialization.OptIn)]
public class Meeting
{
    [JsonProperty]
    public string name { get; set; }
    [JsonProperty]
    public string desc { get; set; }
    [JsonProperty]
    public List<Value> values { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class Value
{
    [JsonProperty]
    public string id { get; set; }
    [JsonProperty]
    public DateTime from { get; set; }
    [JsonProperty]
    public DateTime to { get; set; }
    [JsonProperty]
    public string desc { get; set; }
    [JsonProperty]
    public string customClass { get; set; }
}

[WebMethod(Description = "simple test")]
    public string JSON_Test()
    {
        Meeting m1 = new Meeting { name = "one",
        desc = "ole",
        values = new List<Value> { 
            new Value { customClass = "1", desc = "no desc", from = new DateTime(2011, 12, 29, 0, 0, 0, DateTimeKind.Utc), to = new DateTime(2011, 12, 29, 0, 0, 0, DateTimeKind.Utc), id = "3" },
            new Value { customClass = "2", desc = "no desc1", from = new DateTime(2011, 12, 19, 0, 0, 0, DateTimeKind.Utc), to = new DateTime(2011, 12, 20, 0, 0, 0, DateTimeKind.Utc), id = "4" }
        } 
    };
        Meeting m2 = new Meeting
        {
            name = "second",
            desc = "desc",
            values = new List<Value> { 
            new Value { customClass = "1", desc = "no desc", from = new DateTime(2011, 11, 29, 0, 0, 0, DateTimeKind.Utc), to = new DateTime(2011, 12, 29, 0, 0, 0, DateTimeKind.Utc), id = "3" },
            new Value { customClass = "2", desc = "no desc1", from = new DateTime(2011, 11, 19, 0, 0, 0, DateTimeKind.Utc), to = new DateTime(2011, 12, 20, 0, 0, 0, DateTimeKind.Utc), id = "4" }
        }
        };
        List<Meeting> meetings = new List<Meeting>();
        meetings.Add(m1);
        meetings.Add(m2);

        JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };

        return JsonConvert.SerializeObject(meetings, microsoftDateFormatSettings);
    }

我现在看起来像这样(萤火虫输出):

{"d":"[{\"name\":\"one\",\"desc\":\"ole\",\"values\":[{\"id\":\"3\",\"from\":\"\\/Date(1325116800000)\\/\",\"to\":\"\\/Date(1325116800000)\\/\",\"desc\":\"no desc\",\"customClass\":\"1\"},{\"id\":\"4\",\"from\":\"\\/Date(1324252800000)\\/\",\"to\":\"\\/Date(1324339200000)\\/\",\"desc\":\"no desc1\",\"customClass\":\"2\"}]},{\"name\":\"second\",\"desc\":\"desc\",\"values\":[{\"id\":\"3\",\"from\":\"\\/Date(1322524800000)\\/\",\"to\":\"\\/Date(1325116800000)\\/\",\"desc\":\"no desc\",\"customClass\":\"1\"},{\"id\":\"4\",\"from\":\"\\/Date(1321660800000)\\/\",\"to\":\"\\/Date(1324339200000)\\/\",\"desc\":\"no desc1\",\"customClass\":\"2\"}]}]"}

如果是手动修改的工作示例:

[{ "desc": "ole",
"name": "one",
"values": [{ "customClass": "1",
    "desc": "no desc",
    "from": "/Date(1325116800000)/",
    "id": "3",
    "to": "/Date(1325116800000)/"
},
    { "customClass": "2",
        "desc": "no desc1",
        "from": "/Date(1324252800000)/",
        "id": "4",
        "to": "/Date(1324339200000)/"
    }
  ]
}]

出于某种原因,我的服务添加了额外的 {"d":" 和一些斜线。 我试图通过构建自定义序列化程序来解决这个问题,但我被卡住了。

如何解决开头多余的 d" 和斜线的问题? 也许我正在尝试重新发明轮子,这只是一个简单的设置。

编辑

这是读取 json 数据的 jQuery.Gantt 插件的代码:

    var core = {
        /**
        * Create header
        */
        create: function(element) {
            /**
            * Retrieve data
            */
            if (typeof (settings.source) == 'object') {

                element.data = settings.source;
                core.init(element);

            } else {

                $.ajaxSetup({ scriptCharset: "utf-8", contentType: "application/json; charset=utf-8" });
                $.getJSON(settings.source, function(jsData) {
                    element.data = jsData;
                    core.init(element);
                });
            }

        },

如何将其更改为与 d 一起正常工作?

EDIT2

让它像这样工作:

创建:函数(元素){ /** * 检索数据 */ if (typeof (settings.source) == 'object') {

            if (settings.source.hasOwnProperty("d"))
                element.data = settings.source.d;
            else
                element.data = settings.source;
            core.init(element);
            } else {

                $.ajaxSetup({
                    scriptCharset: "utf-8",
                    contentType: "application/json; charset=utf-8"
                });
                $.getJSON(settings.source, function(jsData) {

                    if (jsData.hasOwnProperty("d"))
                        element.data = jsData.d;
                    else
                        element.data = jsData;
                    core.init(element);
                });
            }

        }

但不知何故,我的网络服务正在返回 xml。 我添加了:

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

但是在查看萤火虫时,我得到了这个:

<?xml version="1.0" encoding="utf-8"?>
 <ArrayOfMeeting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.site.pl/">
<Meeting>
<name>one</name>
<desc>ole</desc>
<values>
  <Value>
    <id>3</id>
    <from>2011-12-29T00:00:00Z</from>
    <to>2011-12-29T00:00:00Z</to>
    <desc>no desc</desc>
    <customClass>1</customClass>
  </Value>
  <Value>
    <id>4</id>
    <from>2011-12-19T00:00:00Z</from>
    <to>2011-12-20T00:00:00Z</to>
    <desc>no desc1</desc>
    <customClass>2</customClass>
  </Value>
</values>
</Meeting>
<Meeting>
<name>second</name>
<desc>desc</desc>
<values>
  <Value>
    <id>3</id>
    <from>2011-11-29T00:00:00Z</from>
    <to>2011-12-29T00:00:00Z</to>
    <desc>no desc</desc>
    <customClass>1</customClass>
  </Value>
  <Value>
    <id>4</id>
    <from>2011-11-19T00:00:00Z</from>
    <to>2011-11-20T00:00:00Z</to>
    <desc>no desc1</desc>
    <customClass>2</customClass>
  </Value>
</values>
</Meeting>
</ArrayOfMeeting>

【问题讨论】:

    标签: asp.net json web-services json.net


    【解决方案1】:

    我认为这里有几个问题。

    1. webmethod的响应不需要json化,asp.net帮你搞定。

    2. ASP.net webservices 用这个 d 父级包装响应,所以你可能需要做这样的事情来把字符串变成一个对象。

      var data = eval(response.d)

      $(".gantt").gantt({source: data})

    OK eval 在这里可能不起作用,所以试试 - http://api.jquery.com/jQuery.parseJSON/

    自己更新调用 $.getJSON **

    好的,我知道现在发生了什么。如果您使用 ASP.NET webservices / page 方法,我相信您将始终拥有 d 包装器。

    您需要自己调用 $.getJSON 并获取响应,然后将其传递给 create 方法。

    【讨论】:

    • 我正在使用 Visual Studio 调试器调试我的 json 结果。结果是正确的,但是返回时会添加额外的字符。
    • 我认为问题在于服务也将响应包装在 JSON 中。所以你是 JSON'ing 两次。编写一个返回会议数组的方法,您可能仍然拥有 d,但其中的对象结构正确。
    • 我已经尝试过了,但是添加了 d 并且对于我得到的每个对象:__type 属性。这个可能很好,但我不知道如何更改甘特插件。我已经编辑了我的问题,显示了它的部分代码。
    • 得到了这个工作,感谢帮助:) 我不得不使用 ajax 而不是 getJSON。
    猜你喜欢
    • 1970-01-01
    • 2015-02-28
    • 2015-06-20
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多