Key and value in Json

2019-07-23 04:23发布

This is how I work right now to build up membership on the website via paypal.

I would like to get done like that here:

   "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"
            }
          }
        ]

What I have done than to now in my MVC site it is like this:

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",

        };

Class to 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;
    }
}

The problem is when I go into my plan and make multiple object which will also be shown from the code paypal made.

The fault lies as said by Plan.

enter image description here

标签: c# json paypal
1条回答
2楼-- · 2019-07-23 05:15

You need to set your public string Plan { get; set; } object to be a string[] string array.

Like this: public string[] Plan { get; set; }

This is why you are getting this red squiggly, as your code is trying to set a normal string to an array of strings.

Hope this helps!

EDIT Also, after more looking, I realised you are trying to add strings to your string array incorrectly.

Syntax for adding strings is like this:

string[] array = new string[] { "string 1", "string 2" };

Where you are creating your strings, and using other properties/variables to create them, ensure you are creating the string itself correctly. Close off your string and add + to append strings from methods or properties. Likewise, to add to a string from a method or property, add + " extra string stuff".

Like this

string[] array = new string[] {
                      "id: " + YourClass.Id,
                      "another string: " + myLocalVariable,  
                      "yet another " + AClass.Property + " class" };

Your code where setting jsonpaypal.Plan should be...

jsonpaypal.Plan = new string[] {
                       "id: " + Convert.ToString(id + orderid),
                       "State: ACTIVE" };

Hope this helps!

查看更多
登录 后发表回答