反序列化JSON对象与JSON.NET

问题描述:

我在这里面临的一个问题,我真的不能找到一种方法,能够带出一个Web方法反序列化JSON对象与JSON.NET

ASPX代码

我下面的JSON对象的值
 $(document).ready(function() { 
     // Add the page method call as an onclick handler for the div. 
     $("#Button1").click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/MethodCall", 
       data: '{ 

“呼叫”:“{ “类型”: “U”, “PARAMS”: { “名称”: “约翰”, “位置”: “CTO” } ] } }', contentType:“application/json;字符集= UTF-8" , 数据类型: “JSON”, 缓存:真,

   success: function (msg) { 
        // Replace the div's content with the page method's return. 
        $("#Result").text(msg.d); 
       }, 
       error: function (xhr, status, error) { 
        // Display a generic error for now. 
        var err = eval("(" + xhr.responseText + ")"); 

        alert(err.Message); 
       } 

      }); 
     }); 
    }); 

ASPX.CS代码

[WebMethod] 
public static string MethodCall(JObject Call) 
{ 





    return "Type of call :"+ Call.Type + "Name is :" + Call.Params.Name + "Position is :" 
    Call.Params.Position ; 







} 

感谢了很多先进的

如果您在输入参数中指定了匹配类型,则页面方法会自动为您反序列化JSON。根据你的榜样数据串,这样的事情:

public class CallRequest 
{ 
    public string Type; 
    public Dictionary<string, string> Params; 
} 

public static string MethodCall(CallRequest Call) 
{ 
    return "Type of call: " + Call.Type + 
     "Name is: " + Call.Params["Name"] + 
     "Position is: " + Call.Params["Position"]; 
} 

我用一本字典,因为你提到的灵活性。如果Params是可预测的,那么可以使用正式类型而不是字典。然后,你可以“点”到Param的属性中而不是引用Dictionary键。

+0

这是最简单的我正在寻找感谢很多人:) – DevMania 2010-07-06 15:29:29

我。不确定我是否遵循你的代码(是你的班级的JObject?),但是如果你使用的是Json.NET(如你的问题所述),请看序列化示例(从http://james.newtonking.com/projects/json-net.aspx):

Product product = new Product(); 
product.Name = "Apple"; 
product.Expiry = new DateTime(2008, 12, 28); 
product.Price = 3.99M; 
product.Sizes = new string[] { "Small", "Medium", "Large" }; 

string json = JsonConvert.SerializeObject(product); 
//{ 
// "Name": "Apple", 
// "Expiry": new Date(1230422400000), 
// "Price": 3.99, 
// "Sizes": [ 
// "Small", 
// "Medium", 
// "Large" 
// ] 
//} 

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json); 

给出一个JSON字符串,它可以反序列化到它所代表的类的实例。

+0

感谢回复,Jobject是json来自客户端,我只是想要访问Json值而不是将它们转换为对象,例如我只想访问Name Value或尺寸数组 – DevMania 2010-07-05 06:37:20

+0

您正在发布JSON **字符串**,这是正确的,所以我不确定为什么您的方法正在接受某些类型的JObject。我认为没有先将它转换为对象就说'要访问Json值'是没有道理的。据我所知,这种弱类型的数据结构可以在JavaScript中完成,而不是C#。您是否试图使用Newtonsoft Json.NET作为问题标题所暗示的? – JustinStolle 2010-07-05 18:11:10

+0

非常感谢您为我解释:) – DevMania 2010-07-06 15:28:17

遵循您的示例代码,如果您在客户端上执行以下jQuery JavaScript(将contentType保留为默认值);

$(document).ready(function() { 
    // Add the page method call as an onclick handler for the div. 
    $("#Button1").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "Default.aspx/MethodCall", 
     data: { call: '{ "Type": "U", "Params": { "Name": "John", "Position": "CTO"} }' }, 
     //contentType: "application/x-www-form-urlencoded", 
     dataType: "json", 
     cache: true, 
     success: function(msg) { 
     // Replace the div's content with the page method's return. 
     $("#Result").text(msg.d); 
     }, 
     error: function(xhr, status, error) { 
     // Display a generic error for now. 
     var err = eval("(" + xhr.responseText + ")"); 

     alert(err.Message); 
     } 

    }); 
    }); 
}); 

你可能做这样的事情在服务器端,假设使用Json.NET的(在http://json.codeplex.com/发现),但你有你的字符串反序列化到一个对象:

using Newtonsoft.Json; 

public class JsonMethodCallObject { 
    public string Type { get; set; } 
    public System.Collections.Hashtable Params { get; set; } 
} 

[WebMethod] 
public static string MethodCall(string call) { 
    try { 
    JsonMethodCallObject deserializedObject = JsonConvert.DeserializeObject<JsonMethodCallObject>(call); 
    return JsonConvert.SerializeObject(new { 
     d = "Type of call: " + deserializedObject.Type + 
     ", Name is: " + deserializedObject.Params["Name"] + 
     ", Position is: " + deserializedObject.Params["Position"] 
    }); 
    } catch (Exception ex) { 
    return JsonConvert.SerializeObject(new { d = ex.Message }); 
    } 
} 
+0

感谢您向我展示另一种方式:) – DevMania 2010-07-06 15:29:08