C# Json中的键和值
这就是我现在通过贝宝在网站上建立会员资格的方式 我想在这里这样做:C# Json中的键和值,c#,json,paypal,C#,Json,Paypal,这就是我现在通过贝宝在网站上建立会员资格的方式 我想在这里这样做: "plan": { "id": "P-rtfhfedh", "state": "ACTIVE", "name": "T-Shirt of the Month Club Plan", "description": "Template creation.", "type": "FIXED", "payment_definitions
"plan": {
"id": "P-rtfhfedh",
"state": "ACTIVE",
"name": "T-Shirt of the Month Club Plan",
"description": "Template creation.",
"type": "FIXED",
"payment_definitions": [
{
"id": "PD-50606817NF8063316RWBZEUA",
"name": "Regular Payments",
"type": "REGULAR",
"frequency": "Month",
"amount": {
"currency": "USD",
"value": "100"
}
}
]
我现在在MVC网站上所做的是:
int orderid = Convert.ToInt32(HelperClass.Helper.Settings.NyTal(8));
JsonPaypal jsonpaypal = new JsonPaypal();
jsonpaypal.IdValue = id + orderid;
jsonpaypal.Name = "Medlemskab";
jsonpaypal.description = "Medlemskab - Orderid: " + orderid;
jsonpaypal.start_date = DateTime.Now;
jsonpaypal.Plan = new string[] {
"id:" Convert.ToString(id + orderid),
"State": "ACTIVE",
};
string[] array = new string[] { "string 1", "string 2" };
类到JsonPaypal
public class JsonPaypal
{
public int IdValue
{
get; set;
}
public string Name
{
get; set;
}
public string description
{
get; set;
}
public DateTime start_date
{
get; set;
}
public string payerURL
{
get; set;
}
public string Plan
{
get; set;
}
public string URL
{
get; set;
}
public string Obj
{
get; set;
}
}
问题是当我进入我的计划,使多个对象,这也将显示在代码贝宝
正如计划所说,故障在于此。
jsonpaypal.Plan = new string[] {
"id: " + Convert.ToString(id + orderid),
"State: ACTIVE" };
您需要将
公共字符串计划{get;set;}
对象设置为字符串[]
字符串数组
如下所示:publicstring[]Plan{get;set;}
这就是为什么您的代码试图将一个正常的字符串
设置为一个字符串
数组时,会出现这个红色的波形
希望这有帮助
编辑另外,在仔细查看之后,我意识到您试图将字符串错误地添加到字符串数组中
添加字符串的语法如下所示:
int orderid = Convert.ToInt32(HelperClass.Helper.Settings.NyTal(8));
JsonPaypal jsonpaypal = new JsonPaypal();
jsonpaypal.IdValue = id + orderid;
jsonpaypal.Name = "Medlemskab";
jsonpaypal.description = "Medlemskab - Orderid: " + orderid;
jsonpaypal.start_date = DateTime.Now;
jsonpaypal.Plan = new string[] {
"id:" Convert.ToString(id + orderid),
"State": "ACTIVE",
};
string[] array = new string[] { "string 1", "string 2" };
在创建字符串并使用其他属性/变量创建字符串时,请确保正确创建字符串本身。关闭字符串并添加+
以附加方法或属性中的字符串。同样,要从方法或属性添加到字符串,请添加+“额外字符串填充”
像这样
string[] array = new string[] {
"id: " + YourClass.Id,
"another string: " + myLocalVariable,
"yet another " + AClass.Property + " class" };
设置jsonpaypal.Plan
的代码应该是…
jsonpaypal.Plan = new string[] {
"id: " + Convert.ToString(id + orderid),
"State: ACTIVE" };
希望这有帮助 id:“Convert.ToString(id+orderid)->“id”:Convert.ToString(id+orderid)它给我仍然是红色的bug,并写“语法错误”,“预期”。当我在乎它的时候,它就会改变@德米特里扎佩瓦洛维特只是第一眼看到的。再看一眼:JsonPaypal.Plan是字符串,您正在尝试分配字符串的
数组。另外,数组
初始化值用分割代码>不能有:
。我建议您创建子类JsonPaypal.Plan and with all fields并填充它。@DmitriyZapevalov那么您想对我创建的类执行类操作吗?我建议表示整个JSON
类层次结构。因此,顶级JsonPaypal
的字段
不仅仅是字符串,而是最新类JsonPaypal.Plan的实例。我刚刚重新查看了您的代码并编辑了上面的答案。相信我,这会有帮助的。它以前也失败过,因为您的字符串[]
格式不正确。看一看,;我已经包括了一些示例。您的代码{“id:”Convert.ToString(id+orderid),“State:”ACTIVE“,}
是错误的。它必须这样写:{“id:+Convert.ToString(id+orderid),“State:ACTIVE”}代码>以正常工作。我已将此代码添加到我的答案中。很高兴能为您提供帮助,这是示例中类似的最后一页小问题。从paypal开始,我对计划中的“payment_定义”有何看法?创建一个映射到Json对象中相同属性的PaymentDefinitions
类(就像您对“JsonPaypal”对象所做的那样)。然后,在JsonPaypal
对象中,添加一个名为PaymentDefinitions
的列表类型的新属性。无论从何处转换(我使用Newtonsoft.Json的ConvertToObject
方法),都将直接按照您的需要进行转换。