English 中文(简体)
Json 分析序列化对象中横刀的 Json 分析错误
原标题:Json parse error with bashslash in the serialized object

I am calling a server side function to return a json format string and parse in client side using javascript and ajax. I got parse error at javascript. I think it is the backslash JavaScriptSerializer adds to serialize object. here is the response I see from firebug: {"d":"{"Item":"Testing"}"} , i understand the backslash is to escape the double quote but how do i get json to fix this problem?? i spend 3 days and do all the search at google. It seems i am doing the same way as others. Thanks for help.

服务器侧边代码 :

[System.Web.Services.WebMethod]
public static string testmethod(string serial)
{ 
    ItemList itemlist = new ItemList();
    itemlist.Item = "Testing";     
    return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(itemlist);
}

[System.Runtime.Serialization.DataContract]
public class ItemList
{
    [System.Runtime.Serialization.DataMember]
    public string Item { get; set; }
}

侧面 Javascript 客户端, 带有 ajax :

function PassParemeterToAspxUsingJquery(serial)
{               
    var sn = "test";//serial;
    $.ajax({
    type: "POST",
    url: "test.aspx/testmethod",
    contentType: "application/json; charset=utf-8",
    data: "{serial: " + sn+" }" ,
    dataType: "json",
    success: function(msg) {           
       alert(msg.d);             
    },
    error: function(jqXHR, textStatus, errorThrown){
       alert("The following error occured: "+ textStatus, errorThrown);
       alert(jqXHR.responseText);
    }
    });
}
最佳回答

WebMethod 组合 值作为 JSON 文本的一部分。 如果您想要它作为 JSON 对象而不是 JSON 字符串序列, 您必须返回一个 < code> Object , 而不是一个 < code> String :

[System.Web.Services.WebMethod]
public static object testmethod(string serial)
{
    ItemList itemlist = new ItemList();
    itemlist.Item = "Testing";
    return itemList;
}

虽然这可能需要.NET3.5和ScriptMethodAttribitte :

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object testmethod(string serial)
{ ... }

然后就说:

success: function(msg) {
   alert(msg.d.Item);
}

或者,您应该能够与它合作,例如通过剖析 msg.d :

success: function(msg) {
    var data = $.parseJSON(msg.d);

    alert(data.Item);
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签