Skip to main content
 首页 » 编程设计

c#之为什么 json 返回空值

2024年02月23日20haluo1

我有一个 javascript 函数,它在 asp.net c# 应用程序的代码后面调用一个方法。我将一个数组传递给 JSON.stringify,它返回与下面的 jsonText 值等效的字符串。以下值仅代表返回的记录之一。

var jsonText = "[{\"eacItem\":{\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\",\"AP\":\"201407\",\"EAC\":\"0\"}}]"; 

然后将字符串传入函数,调用后面代码中的方法。

$.ajax({ 
            type: "POST", 
            traditional: true, //might be needed for serialization to work properly 
            url: pagePath + "/" + "TestMethod", 
            data: "{'eacArray':" + jsonText + "}", 
            contentType: "application/json; charset=utf-8", 
            dataType: "json", 
            async: false, 
            success: function (data) { 
                alert("successfully posted data"); 
            }, 
            error: function (data) { 
                alert("failed posted data"); 
                alert(postData); 
            } 
 
        }); 

这是调用后面代码中的方法。

[WebMethod] 
public static void TestMethod(List<eacItem> eacArray) 
{ 
  //do something 
} 

这是与 JSON 数据集关联的类,作为方法的参数类型。

public class eacItem 
{ 
   public string ID { get; set; } 
   public string AP { get; set; } 
   public string EAC { get; set; } 
 
} 

传递到方法中的数据包含记录,但值均为 null,如下面的屏幕截图所示。我不确定为什么会这样,希望得到任何帮助。

请求的附加信息。这是 eacArray 中值的屏幕截图。

这是调试中的JSON字符串

JSON.stringify(eacArray) 的结果在 数据:“{'eacArray':”+ JSON.stringify(eacArray)+“}”,

[{\"eacItem\":{\"AP\":\"201407\",\"EAC\":\"0\",\"ID\":\"dc97d510-cd29-4a23 -af7f-36c0acfa1914\"}},{\"eacItem\":{\"AP\":\"201304\",\"EAC\":\"0\",\"ID\":\"69326e1b -8d69-431f-8a5b-29f8b279d74c\"}},{\"eacItem\":{\"AP\":\"201305\",\"EAC\":\"0\",\"ID\":\"69326e1b-8d69-431f-8a5b-29f8b279d74c\"}},{\"eacItem\":{\"AP\":\"201306\",\"EAC\":\"0\",\"ID\":\"69326e1b-8d69-431f-8a5b-29f8b279d74c\"}}]"

以下是更正后的代码,以防对任何人有所帮助:

         //Iterate through elements on page 
         $('ElementContainingValue').each(function () { 
            var ap = this.attributes.ap.value; 
            var id = this.attributes.id.value; 
            var eac = $(this).val(); 
            var eacItem = { "AP": ap, "EAC": eac, "ID": id }; 
            eacArray.push(eacItem); 
 
        }); 
 
        var pagePath = window.location.pathname; 
        $.ajax({ 
            type: "POST", 
            traditional: true, //might be needed for serialization to work properly 
            url: pagePath + "/" + "TestMethod", 
            data: "{'eacArray':" + JSON.stringify(eacArray) + "}", 
            // Contents of eacArray after stringify 
            // "[{\"AP\":\"201407\",\"EAC\":\"0\",\"ID\":\"dc97d510"},{\"AP\":\"201408\",\"EAC\":\"0\",\"ID\":\"dc97d510-cd29-4a23-af7f-36c0acfa1914\"}] 
            contentType: "application/json; charset=utf-8", 
            dataType: "json", 
            async: false, 
            success: function (data) { 
                alert("successfully posted data"); 
            }, 
            error: function (data) { 
                alert("failed posted data"); 
                alert(postData); 
            } 
        }); 

这是后面代码中调用的方法。

    [WebMethod] 
    public static void TestMethod(List<eacItem> eacArray) 
    { 
        foreach (var eacItem in eacArray) 
        { 
            string ap = eacItem.AP; 
            string id = eacItem.ID; 
            string eac = eacItem.EAC; 
        } 
    } 

这是与数组关联的类。

  public class eacItem 
  { 
    public string ID { get; set; } 
    public string AP { get; set; } 
    public string EAC { get; set; } 
 
  } 

请您参考如下方法:

您的 POST 数据是:{'eacArray':"+ jsonText + "}" 不包含任何可映射到 eacItem 的信息(没有匹配属性等)。

验证您是否在请求中传递了 JSON.stringify 的结果(可能使用 Fiddler 或 Wireshark)。您需要确保您希望通过网络传输的数据与您希望发送的数据相匹配。