【问题标题】:return json as key pair format with asp.net web service使用 asp.net Web 服务将 json 作为密钥对格式返回
【发布时间】:2015-01-18 12:10:35
【问题描述】:

我用 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。我知道从网络服务返回的不应该是文本,我想成为:

 [{"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}}]

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

【问题讨论】:

  • 谢谢,但你能解释更多吗?你的链接不喜欢我的问题。我想定义键、值、容量、最大容量。
  • 这取决于你想在哪里确定它,在 Jacascript 中还是在哪里?
  • 我想在 asp.net 网络服务中确定 maxcapacity,capacity,...。
  • 为什么要以这种复杂的格式返回 JSON?为什么不用{"video":"sample code","product":"sample code 2"} 表示单个结果或[{"video":"sample code","product":"sample code 2"}] 表示多个结果?还有,你是问我们怎么用c#写webservice代码,还是用javascript写jquery代码?

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


【解决方案1】:

我假设您想将 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,你会得到:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多