当我尝试在newtonsoft.json中使用Jsonserialize时,返回null到字符串

时间:2016-05-18 04:26:03

标签: c# json serialization json.net

就像我说的,我不能使用JsonConvert.SerializeObject(b)将字符串作为返回。

这就是我正在尝试的:

        BankAccount b = new BankAccount();
        b.Agencia = "0192";
        b.AgenciaDv = "0";
        b.Conta = "03245";
        b.ContaDv = "0";
        b.BankCode = "0341";
        b.DocumentNumber = "26268738888";
        b.LegalName = "API BANK ACCOUNT";
        b.Save();

        string json = JsonConvert.SerializeObject(b);

字符串json必须接收json对象,对吗?如何获取bankaccount b对象的json对象?

编辑: 我没有任何例外。 BanckAccount是一个参考库的类。

    using System;
using Newtonsoft.Json;

namespace PagarMe
{
    public class BankAccount : Base.Model
    {
        protected override string Endpoint { get { return "/bank_accounts"; } }

        public string BankCode
        {
            get { return GetAttribute<string>("bank_code"); }
            set { SetAttribute("bank_code", value); }
        }

        public string Agencia
        {
            get { return GetAttribute<string>("agencia"); }
            set { SetAttribute("agencia", value); }
        }

        public string AgenciaDv
        {
            get { return GetAttribute<string>("agencia_dv"); }
            set { SetAttribute("agencia_dv", value); }
        }

        public string Conta
        {
            get { return GetAttribute<string>("conta"); }
            set { SetAttribute("conta", value); }
        }

        public string ContaDv
        {
            get { return GetAttribute<string>("conta_dv"); }
            set { SetAttribute("conta_dv", value); }
        }

        public DocumentType DocumentType
        {
            get { return GetAttribute<DocumentType>("document_type"); }
            set { SetAttribute("document_type", value); }
        }

        public string DocumentNumber
        {
            get { return GetAttribute<string>("document_number"); }
            set { SetAttribute("document_number", value); }
        }

        public string LegalName
        {
            get { return GetAttribute<string>("legal_name"); }
            set { SetAttribute("legal_name", value); }
        }

        public bool ChargeTransferFees
        {
            get { return GetAttribute<bool>("charge_transfer_fees"); }
            set { SetAttribute("charge_transfer_fees", value); }
        }

        public BankAccount()
            : this(null)
        {

        }

    public BankAccount(PagarMeService service)
            : base(service)
        {
        }
    }

我得到的“json”变量的结果是“{}”。 我需要一个json对象来发一个帖子请求。我能做什么?

EDIT²: getAttribute:网络框架的标准方法。 检索元素上命名属性的值。

SetAttribute:网络框架的标准方法。 设置元素上命名属性的值。

Base.Model:

public abstract class Model : AbstractModel
{
    public string Id
    {
        get
        {
            var result = GetAttribute<object>("id");

            if (result == null)
                return null;

            return result.ToString();
        }
        set { SetAttribute("id", value); }
    }
public DateTime? DateCreated
{
    get
    {
        var result = GetAttribute<DateTime?>("date_created");
        if (result == null)
            return null;
        return result;
    }
}
public DateTime? DateUpdated
{
    get
    {
        var result = GetAttribute<DateTime?>("date_updated");
        if (result == null)
            return null;
        return result;
    }
}

    private Model()
        : this(null)
    {
    }

    protected Model(PagarMeService service)
        : base(service)
    {
    }

    public void ExecuteSelfRequest(PagarMeRequest request)
    {
        LoadFrom(request.Execute().Body);
    }

    public async Task ExecuteSelfRequestAsync(PagarMeRequest request)
    {
        LoadFrom((await request.ExecuteAsync()).Body);
    }

    public void Refresh()
    {
        Refresh(Id);
    }

    internal void Refresh(string id)
    {
        if (id == null)
            throw new InvalidOperationException("Cannot refresh not existing object.");

        var request = CreateCollectionRequest("GET", "/" + id);
        var response = request.Execute();

        LoadFrom(response.Body);
    }

    #if HAS_ASYNC
    public async Task RefreshAsync()
    {
        await RefreshAsync(Id);
    }

    internal async Task RefreshAsync(string id)
    {
        if (id == null)
            throw new InvalidOperationException("Cannot refresh not existing object.");

        var request = CreateCollectionRequest("GET", "/" + id);
        var response = await request.ExecuteAsync();

        LoadFrom(response.Body);
    }
    #endif

    public void Save()
    {
        if (Id == null)
        {
            var request = CreateCollectionRequest("POST");

            request.Body = ToJson(SerializationType.Full);

            var response = request.Execute();

            LoadFrom(response.Body);
        }
        else
        {
            var request = CreateRequest("PUT");

            request.Body = ToJson(SerializationType.Shallow);

            var response = request.Execute();

            LoadFrom(response.Body);
        }
    }

    #if HAS_ASYNC
    public async Task SaveAsync()
    {
        if (Id == null)
        {
            var request = CreateCollectionRequest("POST");

            request.Body = ToJson(SerializationType.Full);

            var response = await request.ExecuteAsync();

            LoadFrom(response.Body);
        }
        else
        {
            var request = CreateRequest("PUT");

            request.Body = ToJson(SerializationType.Shallow);

            var response = await request.ExecuteAsync();

            LoadFrom(response.Body);
        }
    }
    #endif

    protected PagarMeRequest CreateRequest(string method, string endpoint = "")
    {
        return new PagarMeRequest(Service, method, Endpoint + "/" + Id + endpoint);
    }

    protected PagarMeRequest CreateCollectionRequest(string method, string endpoint = "")
    {
        return new PagarMeRequest(Service, method, Endpoint + endpoint);
    }

    internal void SetId(string id)
    {
        Id = id;
    }

    protected virtual bool CanSave()
    {
        return true;
    }

    protected abstract string Endpoint { get; }
}

认为错误就像配置一样。 Newtonsoft.Json需要在模型类中使用字段声明的特定配置吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题。 BankAccount类在另一个解决方案中。引用了DLL,但是newtonsoft.json无法以这种方式序列化。