JSON未解析为自定义对象 [英] JSON not parsed into custom object

查看:77
本文介绍了JSON未解析为自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见下面的AJAX:

Please see the AJAX below:

 <script type="text/javascript"  src="Javascript/json2.js"></script>
    <script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
    <script type = "text/javascript">
        function GetData() {
            $.ajax({
                type: "POST",
                url: "JSONExample.aspx/GetPerson",
            contentType: "application/json; charset=utf-8",
            dataType: "text",
            success: OnSuccess(),
            //async: false,
            failure: function (response) {
                alert('there was an error counting possibles')
            }
        });

        function OnSuccess() {
            return function (response) {
                alert(response);
                window.location.href("JSONExample.aspx?id=" + response);
            }
        }
        }
        GetData()
    </script>

以及下面的服务器端代码:

and the server side code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Newtonsoft.Json;

namespace SerializeAndDeserializeJSON
{
    //[Serializable]
    public class Person
    {
        public String Name;
        public int Age;
    }

    public partial class JSONExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Request.QueryString["id"]== null)==false)
            {
                var json = Request.QueryString["id"];
                var person = JsonConvert.DeserializeObject<Person>(json); //person is null
            }
        }

        [System.Web.Services.WebMethod]
        public static Person GetPerson()
        {
            Person p1 = new Person();
            p1.Name = "Ian";
            p1.Age=35;
            return p1;
        }
    }
}

在页面加载中,页面加载后Person对象的值如下:

In the page load the Person object values are as follows after the page load:

名称:null 年龄:0

Name: null Age: 0

名字应该是Ian,年龄应该是35.我在做什么错了?

The name should be Ian and the Age should be 35. What am I doing wrong?

推荐答案

我在做什么错了?

What am I doing wrong?

尝试将dataType设置为json而不是text:

Try setting dataType to json instead of text:

dataType: 'json'

然后在id参数中将javascript对象作为JSON字符串发送:

And then send the javascript object as a JSON string in the id parameter:

window.location.href("JSONExample.aspx?id=" + encodeURIComponent(JSON.stringify(response.d)));

请注意,由于ASP.NET WebMethods使用此特殊属性序列化了响应,因此我们在此处使用response.d.

Notice that we are using response.d here because ASP.NET WebMethods serialize the responses using this special property.

另外,您可能希望使用公共属性代替模型的字段:

Also you probably want to use public properties instead of fields for your model:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

某些框架使字段窒息.

这篇关于JSON未解析为自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆