正如我所说,我无法使用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对象来进行post请求。我能做什么?
编辑²:getAttribute:网络框架的标准方法。检索元素的命名属性的值。
SetAttribute:网络框架的标准方法。设置元素上命名属性的值。
基本型号:
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; }
}
认为错误与配置类似。Json需要在模型类中对字段声明进行特定配置吗?
我发现了问题。BankAccount类在另一个解决方案中。DLL被引用,但newtonsoft.json不能以这种方式序列化。