简体   繁体   English

如何在C#中检索嵌套的json对象?

[英]How to retrieve nested json objects in C#?

Stripe Transfer JSON 条带传输JSON

I'm trying to get all charge IDs associated with a transfer. 我正在尝试获取与转账相关的所有费用ID。

var json = new StreamReader(context.Request.InputStream).ReadToEnd();

var stripeEvent = StripeEventUtility.ParseEvent(json);

StripeTransfer stripeTransfer;
switch (stripeEvent.Type)
{
    case "transfer.created":
        stripeTransfer = Stripe.Mapper<StripeTransfer>.MapFromJson(stripeEvent.Data.Object.ToString());
        var transferId = stripeTransfer.Id;
        stripeTransfer = _stripeManager.GetTransfer(transferId);

        foreach (var charge in stripeEvent.Data.Object["transactions"])
        {

        }

        _stripeManager.ProcessTransfer(stripeTransfer);
        break;

In visual studio's immediate window, stripeEvent.Data.Object["transactions"] shows the data I want so I know that the json is getting sucked in properly. 在Visual Studio的直接窗口中, stripeEvent.Data.Object["transactions"]显示了我想要的数据,所以我知道json被正确地吸收了。 Those transactions are a collection of charges, they match my .net StripeCharge object. 这些交易是收费的集合,它们与我的.net StripeCharge对象匹配。 I'm having trouble figuring out how to iterate through the transactions...all I really need is the ID for each. 我在弄清楚如何遍历交易时遇到麻烦...我真正需要的只是每个交易的ID。 Would like to see "transactions" as a C# IEnumerable object. 希望将“交易”视为C#IEnumerable对象。

(the json in question is linked at the top of this post) let me know if more info is needed. (有问题的json链接在这篇文章的顶部)让我知道是否需要更多信息。

I've found the specific item is under ["transactions"]["data"][0]["id"] but there may be more than one so, still working on how to get them out and cast them...think I'm close but it seems like there should be a more elegant way of doing it. 我发现特定项目位于["transactions"]["data"][0]["id"]但可能有多个,仍在研究如何将其投放并投放...认为我已经接近了,但似乎应该有一种更优雅的方法。

EDIT, 编辑,

Thanks Andrew, so even though I have all of the charge data, it is incoming data. 谢谢安德鲁,所以即使我拥有所有费用数据,它都是传入数据。 So what I'll be doing is using the event to just get the id and then make the call to get the charge object from my own end to prevent any event spoofs. 因此,我要做的就是使用事件获取ID,然后进行调用以从我自己的一端获取费用对象,以防止发生任何事件欺骗。 So that means I don't have to worry about casting at this point. 因此,这意味着我现在不必担心投射。 Here is my solution, feel free to advise if there is a better way to do it 这是我的解决方案,请随时告知是否有更好的方法

for (int i = 0; i < Int32.Parse(stripeEvent.Data.Object["transactions"]["total_count"].ToString()); i++)
{
     string chargeId = stripeEvent.Data.Object["transactions"]["data"][i]["id"].ToString();
     // do stuff with it
}

just for completeness: Data under transactions looks like an array so should be able to index into them.. 仅出于完整性考虑:事务下的数据看起来像一个数组,因此应该能够对其进行索引。

If you need to access to any other fields in the future you could construct c# objects but given the webhook 3rd party dance you are already doing probably not worth it as you only need id. 如果将来需要访问任何其他字段,则可以构造c#对象,但是考虑到webhook 3rd party舞,您已经不值得这样做,因为您只需要id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM