使用JSON.Net解析某些数据

时间:2016-01-10 17:04:17

标签: c# json linq paypal

我试图从我收到的一些JSON数据中解析某个链接,但我似乎无法做到这一点?这是一些示例数据(来自PayPal API):

{"id":"PAY-3YA6562986829024GK2JH7UQ","intent":"sale","state":"created","payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"12.00","currency":"USD"},"description":"creating a payment","related_resources":[]}],"create_time":"2016-01-10T15:59:14Z","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ","rel":"self","method":"GET"},{"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5CP140577W0453458","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ/execute","rel":"execute","method":"POST"}]}

所以我试图做以下事情:

    dynamic stuff = JsonConvert.DeserializeObject(createdPayment.ConvertToJson());
    string paymentURL = stuff.href;
    MessageBox.Show(paymentURL);

我还尝试使用Newtonsoft.Json.Linq并将stuff.links填充到JAraay中,然后通过索引拉取链接,没有运气。我使用的这种方法没有给我一个错误,它只是为我返回一个空字符串?

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

问题是你的" href"链接在您的"链接"属性,你可以像这样访问它们:

        dynamic stuff = JsonConvert.DeserializeObject(json);

        foreach (var item in stuff.links)
        {
            MessageBox.Show(item.href);
        }

编辑:添加了获取链接列表的示例

        dynamic stuff = JsonConvert.DeserializeObject(json);
        var links = new List<string>();
        foreach (var item in stuff.links)
        {            
            links.Add((string)item.href);
        }

答案 1 :(得分:0)

您应该创建一个包含JSON字符串所需值的类。

该课程将如下所示:

public class Payer
{
    public string payment_method { get; set; }
}

public class Amount
{
    public string total { get; set; }
    public string currency { get; set; }
}

public class Transaction
{
    public Amount amount { get; set; }
    public string description { get; set; }
    public List<object> related_resources { get; set; }
}

public class Link
{
    public string href { get; set; }
    public string rel { get; set; }
    public string method { get; set; }
}

public class RootObject
{
    public string id { get; set; }
    public string intent { get; set; }
    public string state { get; set; }
    public Payer payer { get; set; }
    public List<Transaction> transactions { get; set; }
    public string create_time { get; set; }
    public List<Link> links { get; set; }
}

然后你就可以做这样的事情(使用Newtonsoft):

var object = JsonConvert.DeserializeObject<RootObject>(jsonstring);

然后,您将能够遍历List<Link> links对象并从那里获取href值。

答案 2 :(得分:0)

如果您需要解析数据,那么您只需尝试此示例控制台应用。并在Newtonsoft api reference了解更多信息。

class Program
{
    class HrefResult
    {
        public string Href { get; set; }
        public string Rel { get; set; }
        public string Method { get; set; }
    }

    static void Main(string[] args)
    {
        String createdPayment = @"{""id"":""PAY - 3YA6562986829024GK2JH7UQ"",""intent"":""sale"",""state"":""created"",""payer"":{""payment_method"":""paypal""},""transactions"":[{""amount"":{""total"":""12.00"",""currency"":""USD""},""description"":""creating a payment"",""related_resources"":[]}],""create_time"":""2016 - 01 - 10T15: 59:14Z"",""links"":[{""href"":""https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ"",""rel"":""self"",""method"":""GET""},{""href"":""https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-5CP140577W0453458"",""rel"":""approval_url"",""method"":""REDIRECT""},{""href"":""https://api.sandbox.paypal.com/v1/payments/payment/PAY-3YA6562986829024GK2JH7UQ/execute"",""rel"":""execute"",""method"":""POST""}]}";

        JObject stuff = JObject.Parse(createdPayment);
        IList<Newtonsoft.Json.Linq.JToken> results = stuff["links"].Children().ToList();

        IList<HrefResult> hrefResults = new List<HrefResult>();
        foreach (JToken result in results)
        {
            HrefResult hrefResult = Newtonsoft.Json.JsonConvert.DeserializeObject<HrefResult>(result.ToString());
            hrefResults.Add(hrefResult);
        }

        foreach(var elem in hrefResults) Console.WriteLine("{0}", elem.Href);
    }
}