私が言ったように、JsonConvert.SerializeObject(b)を使って文字列を返すことができません。newtonsoft.jsonでJsonserializeを使用しようとすると、文字列にnullが返される
私がしようとしているものです:
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は、モデルクラスのフィールド宣言の特定の構成を必要としますか?
何か間違っているようですが、予期しない結果が出ましたか? – Ian
コードは正常です。例外はどこにもありますか? – JimBobBennett
@Ian OPは 'json'変数に' null'を返しています。 – niksofteng