【问题标题】:Getting the data inside the C# web service from Jsonified string从 Jsonified 字符串获取 C# Web 服务中的数据
【发布时间】:2011-01-17 18:28:01
【问题描述】:

在我的 JS 中,我使用 Jquery 的 $ajax 函数来调用 Web 服务并将 jsonified 数据发送给它。数据格式如下:

var countries = {

   "1A": {
        id: "1A",
        name: "Andorra"        
    },
    "2B": {
        id: 2B
        name: "Belgium"       
    },

    ..etc
};

var jsonData = JSON.stringify({ data: data });

//然后调用 $ajax 并调用 c# web 服务


在 c# 端,Web 服务需要解压这些数据,目前它以 string[][] 数据类型出现。如何将其转换为格式,以便我可以参考诸如

之类的属性

.id 和 .name?假设我有一个名为 Sample 的类具有这些属性? 谢谢!

编辑:

这是我的 JS 代码:

var jsonData = JSON.stringify(countries);

     $.ajax({
         type: 'POST',
         url: 'http://localhost/MyService.asmx/Foo',
         contentType: 'application/json; charset=utf-8',
         data: jsonData,
         success: function (msg) {                 
             alert(msg.d);
         },
         error: function (xhr, status) {
              switch (status) {
                 case 404:
                     alert('File not found');
                     break;
                 case 500:
                     alert('Server error');
                     break;
                 case 0:
                     alert('Request aborted');
                     break;
                 default:
                     alert('Unknown error ' + status);
             } 
         }
     });

在我拥有的 c# web 服务中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.Script.Services;

[WebMethod]
[ScriptMethod]
public string Foo(IDictionary<string, Country> countries)   
{

    return "success";
}

【问题讨论】:

    标签: c# jquery web-services json


    【解决方案1】:

    我不知道你在说什么 string[][] 以及它来自哪里,但最好使用强类型。所以定义一个自定义类型来为你的数据建模:

    public class Country
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    

    然后让 web 方法获取字典:

    [WebMethod]
    [ScriptMethod]
    public string Foo(IDictionary<string, Country> countries)
    {
        // Here you will have countries["1A"].Name = "Andorra" and 
        // countries["2B"].Name = Belgi
        ...    
        return "success";
    }
    

    然后简单地调用这个方法(查看UPDATE获取正确的数据):

    罢工>

    var data = {
       "1A": {
            id: "1A",
            name: "Andorra"        
        },
        "2B": {
            id: 2B
            name: "Belgi"       
        }
    };
    

    $.ajax({
        type: 'POST',
        url: '/myservice.asmx/Foo',
        contentType: 'application/json; charset=utf-8',
        data: { JSON.stringify(data) },
        success: function(msg) {
            alert(msg.d);
        }
    });
    

    更新:

    其实请求的格式应该是这样的:

    var data = {
        countries: {
            "1A": {
                id: "1A",
                name: "Andorra"
            },
            "2B": {
                id: "2B",
                name: "Belgium"
            }
        }
    };
    

    【讨论】:

    • 谢谢:) 但我也在向这个网络服务发送其他数据。目前,我的网络服务看起来像 [WebMethod] public void Foo(string[][] data) { //do whatever } 在 data 参数中,data[0] 包含凭据,data[1] 包含对象的实际数组....
    • @gnomixa,还有什么其他数据?一切都可以用强类型来表示。因此,定义一个基础对象,该对象包含作为该字典的属性,并将您传递的其他数据作为另一个属性。摆脱这个string[][]。它使您的网络方法无法维护。
    • 当然,您只需要使用强类型对数据进行建模。
    • 好的,我会重新措辞。如果我向 Web 服务发送两种不同类型的变量。 Foo(IDictionary countries) 不起作用,因为它只解包一种类型 - Country....我的理解是否正确?
    • @gnomixa,您不会发送两种不同的类型。您将发送一个包含两个属性的单一类型:Foo(MyModel model) 其中 MyModel 包含 Country 字典和一些其他属性,其中包含您的其他数据。
    【解决方案2】:

    你试过JavaScriptSerializer吗?正如你所说,假设你有一个具有这些属性的类,它应该/可能能够deserialize JSON 字符串到该类型。

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 2017-04-11
      • 1970-01-01
      相关资源
      最近更新 更多