使用asp.net Web服务将json作为密钥对格式返回

时间:2015-01-18 12:10:36

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

我用asp.net和jquery.in写了一个项目,我有一个网页:

  <div id="lookup">
                        <div class="other">
                            <div class="st"></div>
                            <ul class="videos"></ul>
                            <ul class="items"></ul>

在网络服务中我编写此代码以获取搜索关键字并搜索视频和产品集合并将其返回到网页。网络服务的部分是:

 public string GetSearchResult(string keyword, string urlCode)
{
string test = [sample code];
string test2 = [sample code2];
StringBuilder sb = new StringBuilder(test);
StringBuilder sb2 = new StringBuilder(test2);
return @"[
            {
                ""Key"": ""video"",
                ""Value"": {
                    ""m_MaxCapacity"": " + sb.MaxCapacity + @",
                    ""Capacity"": " + sb.Capacity + @",
                    ""m_StringValue"": """ + test + @""",
                    ""m_currentThread"": 0
                }
            },
            {
                ""Key"": ""product"",
                ""Value"": {
                    ""m_MaxCapacity"": " + sb2.MaxCapacity + @",
                    ""Capacity"": " + sb2.Capacity + @",
                    ""m_StringValue"": """ + test2 + @""",
                    ""m_currentThread"": 0
                }
            }]";

}

在jquery中,部分代码是:

function lookUp() {

    n = $("#SearchBox").val().trim(),
    r = "All";
    n.length >= 2 ? ($.ajax({
    url: ServiceUrl + "SearchService.asmx/GetSearchResult",
    data: '{keyword: "' + n + '",urlCode:"' + r + '" }',
    type: "POST",
    contentType: "application/json; charset=utf-8",
    timeout: 15000,
    processData: !1,
    success: function (o) {
        o != null && (r == "All" ? (f.show(), u.show().html(o[2].Value.m_StringValue), .........

此代码必须从网络服务获取视频类别或产品类别中的搜索结果,并使用示例代码或示例代码2填充网页中的视频或项目ul。 这段代码不起作用。我想确定每个m_StringValue。我知道从Web服务返回不应该是文本,我想成为:

 [{"Key":"video","Value":  {"m_MaxCapacity":2147483647,"Capacity":64,"m_StringValue":"sample   code","m_currentThread":0}},
 {"Key":"product","Value":  {"m_MaxCapacity":2147483647,"Capacity":55,"m_StringValue":"sample code    2","m_currentThread":0}}]

但我不知道如何更改网络服务。

1 个答案:

答案 0 :(得分:0)

我假设您要将JSON反序列化为.Net类型。您可以使用JavascriptSerializer。首先创建具有JSON中存在的字段名称和类型的类,然后反序列化为该类型。例如:

using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace loopy
{
  class Program
  {
    static void Main(string[] args)
    {
      var ser = new JavaScriptSerializer();
      string websvcResult = CallWebService();
      var keysAndCapacities = ser.Deserialize<List<ServiceResultClass>>(websvcResult);
    }

    public static string CallWebService()
    {
      //Call web service here, this is just a mock of course.
      return @"[
                {
                    ""Key"": ""products"",
                    ""Value"": {
                        ""m_MaxCapacity"": 2147483647,
                        ""Capacity"": 64,
                        ""m_StringValue"": ""<li>sample products or search results in products category</li>"",
                        ""m_currentThread"": 0
                    }
                },
                {
                    ""Key"": ""news"",
                    ""Value"": {
                        ""m_MaxCapacity"": 2147483647,
                        ""Capacity"": 55,
                        ""m_StringValue"": ""<li class='noresult'>sample news or search results in news category</li>"",
                        ""m_currentThread"": 0
                    }
                }
            ]";
    }
  }

  public class ServiceResultClass
  {
    public string Key { get; set; }
    public Capacities Value { get; set; }
  }

  public class Capacities
  {
    public long m_MaxCapacity { get; set; }
    public string m_StringValue { get; set; }
  }
}

如果你在Main()的末尾打破并检查keysAndCapacities,你会得到这个:

enter image description here