将Json反序列化为C#列表

时间:2014-11-20 09:18:06

标签: c# json json.net

我正在尝试将JSON反序列化为C#列表。我有Jquery代码,

    $(document).ready(function () {
        $("#btn_check").click(function () {
            var str="";
            x = $("#frm").serializeArray();
            $("#Label1").empty();
            $.each(x, function (i, field) {
              //  $("#Label1").append(field.name + ":" + field.value + " ");

                str = str + field.name + ":" + field.value + " ";

            });
            var jsonstr=JSON.stringify(str);            });
    });
在stringify之后,我得到一些奇怪的东西,如jsonstr = __VIEWSTATE:/ wEPDwUKLTg1MjI5MDU0MWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFjIFB2NoZWNrXzAFB2NoZWNrXzEFB2NoZWNrXzIFB2NoZWNrXzMFB2NoZWNrXzQFB2NoZWNrXzUFB2NoZWNrXzYFB2NoZWNrXzcFB2NoZWNrXzgFB2NoZWNrXzkFCGNoZWNrXzEwBQhjaGVja18xMQUIY2hlY2tfMTIFCGNoZWNrXzEzBQhjaGVja18xNAUIY2hlY2tfMTUFCGNoZWNrXzE2BQhjaGVja18xNwUIY2hlY2tfMTgFCGNoZWNrXzE5BQhjaGVja18yMAUIY2hlY2tfMjEFCGNoZWNrXzIyBQhjaGVja18yMwUIY2hlY2tfMjQFCGNoZWNrXzI1BQhjaGVja18yNgUIY2hlY2tfMjcFCGNoZWNrXzI4BQhjaGVja18yOQUIY2hlY2tfMzAFCGNoZWNrXzMxBQhjaGVja18zMgUIY2hlY2tfMzMFCGNoZWNrXzM0BQhjaGVja18zNQUIY2hlY2tfMzYFCGNoZWNrXzM3BQhjaGVja18zOAUIY2hlY2tfMzkFCGNoZWNrXzQwBQhjaGVja180MQUIY2hlY2tfNDIFCGNoZWNrXzQzBQhjaGVja180NAUIY2hlY2tfNDUFCGNoZWNrXzQ2BQhjaGVja180NwUIY2hlY2tfNDgFCGNoZWNrXzQ5s98N0sYArkR3uk7Sb4bJWOocOpU = __VIEWSTATEGENERATOR:172284EE __EVENTVALIDATION:/ wEWNAKauo6nDAK5rJ0YAp7D / 4IGAoPa4e0LAujww9gBAs2HpsMHArKeiK4NApe16pgDAvzLzIMJApH2i8IBAvaM7qwHAp7Dv7UBAp7Dq9oIAp7Dl / 8PAp7Dg6QHAp7Dj6IEAp7D + 8YLAp7D5 + sCAp7D05AKAp7Dn9wLAp7Di4EDAoPaoaAHAoPajcU OAoPa + ekFAoPa5Y4NAoPa8YwKAoPa3bEBAoPaydYIAoPatfsPAoPagccBAoPa7esIAujwg4sNAujw768EAujw29QLAujwx / kCAujw0 / cPAujwv5wHAujwq8EOAujwl + YFAujw47EHAujwz9YOAs2H5vUCAs2H0poKAs2Hvr8BAs2HquQIAs2HtuIFAs2HoocNAs2HjqwEAs2H + tALAs2HxpwNAs2HssEEAvCFnqIPppJbhAvF8AzSoMd / uZfiRXpzWu0 = check_0:上check_1:上check_2:上check_3:上check_4:上check_5:上check_6:上check_7:上check_8:上check_9:上check_10:上check_11:on check_12:on check_14:on check_14:on check_16:on check_17:on check_18:on check_19:on check_20:on check_21:on check_22:on check_23:on check_24:on check_25:on check_26:on check_27:在check_28:on check_30:on check_30:on check_32:on check_32:on check_34:on check_35:on check_36:on check_37:on check_38:on check_39:on check_40:on check_41:on check_42:on check_43:on check_43:on check_44 :on check_45:on check_46:on check_47:on check_48:on check_49:on

check_0 to check_1是动态创建的复选框,我想以C#作为列表或任何形式获取它们的值来解释并将它们推送到数据库中。

asp.net:

<form id="frm" runat="server">

    <asp:Panel ID="pnl_seat" runat="server">
        <asp:PlaceHolder ID="plhdr_seat" runat="server">
         checkboxes are dynamically created here
       </asp:PlaceHolder>
        <br />
        <button id="btn_check" type="button">Serialize form values</button>
        <asp:Button ID="btn_submit" runat="server" Text="Submit"  OnClick="btn_submit_Click" />
        <br />

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />

    </asp:Panel>

</form>

图片:enter image description here

C#代码:

 using Newtonsoft.Json;
class abc // some class
{   
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        List<Test> myDeserializedObjList = (List<Test>)   Newtonsoft.Json.JsonConvert.DeserializeObject(Request["jsonstr"], typeof(List<Test>));
    }
 }

上面的代码我是从codeproject.com得到的。 但是在运行此代码时,我得到一个异常,即“str”为“null”。如何将label1或str推送到服务器端?请帮帮我。

2 个答案:

答案 0 :(得分:0)

你能粘贴更多代码吗?从有限的源代码中发现任何问题都很难。  您可以使用ajax请求将str发送到服务器: $.get('your url', {str: str})

顺便说一下,似乎str不是有效的JSON字符串。 str = JSON.strigify(x)会将对象字符串化为JSON字符串。


试试这个: ```

$(document).ready(function () {
    $("#btn_check").click(function () {
        var x = $("#frm").serializeArray();
        $("#Label1").empty().text(JSON.stringify(x));
    });
});

using Newtonsoft.Json;
class abc // some class
{
    protected void btn_submit_Click(object sender, EventArgs e)
    {
    List<Test> myDeserializedObjList = (List<Test>) Newtonsoft.Json.JsonConvert.DeserializeObject(Label1.Text, typeof(List<Test>));
    }
}

```

答案 1 :(得分:0)

JQUERY:

 $(document).ready(function () {
        $("#btn_check").click(function () {
            var str = "";
            x = $("#frm").serializeArray();
             str = JSON.stringify(x);
           // $("#HiddenField1").Value(str);
            $('#<%=hdnSelectedTicket.ClientID %>').val(str);
           <%-- $('#<%=hdnSelectedTicket.ClientID %>')--%>

        });
    });

ASP.NET:

<form id="frm" runat="server">
     <asp:HiddenField ID="hdnSelectedTicket"  runat="server" />
    <asp:Panel ID="pnl_seat" runat="server">
        <asp:PlaceHolder ID="plhdr_seat" runat="server"></asp:PlaceHolder>
        <br />
        <button id="btn_check" type="button">Serialize form values</button>
        <asp:Button ID="btn_submit" runat="server" Text="Submit" OnClick="btn_submit_Click" />
        <br />

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />


    </asp:Panel>

</form>

C#:

 using Newtonsoft.Json;
protected void btn_submit_Click(object sender, EventArgs e)
    {
        //string a = hdnSelectedTicket.Value;
        List<Test> myDeserializedObjList = (List<Test>)Newtonsoft.Json.JsonConvert.DeserializeObject    (hdnSelectedTicket.Value, typeof(List<Test>));

    }