繁体   English   中英

AWS EC2读取商品文件JSON文件

[英]AWS EC2 Reading an Offer File JSON file

我正在尝试解析AWS EC2读取商品文件JSON文件,但在读取文件时遇到问题。 正如您在JSON文件中看到的那样,这里有一个产品列表,并且JSON文件的键值每次都不同,我无法读取它。 任何想法如何读取文件?

"Reserved" : {
  "DQ578CGN99KG6ECF" : {
    "DQ578CGN99KG6ECF.HU7G6KETJZ" : {
      "offerTermCode" : "HU7G6KETJZ",
      "sku" : "DQ578CGN99KG6ECF",
      "effectiveDate" : "2017-02-28T23:59:59Z",
      "priceDimensions" : {
        "DQ578CGN99KG6ECF.HU7G6KETJZ.2TG2D8R56U" : {
          "rateCode" : "DQ578CGN99KG6ECF.HU7G6KETJZ.2TG2D8R56U",
          "description" : "Upfront Fee",
          "unit" : "Quantity",
          "pricePerUnit" : {
            "USD" : "11213"
          },
          "appliesTo" : [ ]
        },
        "DQ578CGN99KG6ECF.HU7G6KETJZ.6YS6EN2CT7" : {
          "rateCode" : "DQ578CGN99KG6ECF.HU7G6KETJZ.6YS6EN2CT7",
          "description" : "Windows (Amazon VPC), hs1.8xlarge instance-hours used this month",
          "beginRange" : "0",
          "endRange" : "Inf",
          "unit" : "Hrs",
          "pricePerUnit" : {
            "USD" : "1.2510000000"
          },
          "appliesTo" : [ ]
        }
      },
      "termAttributes" : {
        "LeaseContractLength" : "1yr",
        "OfferingClass" : "standard",
        "PurchaseOption" : "Partial Upfront"
      }
    },
    "DQ578CGN99KG6ECF.38NPMPTW36" : {
      "offerTermCode" : "38NPMPTW36",
      "sku" : "DQ578CGN99KG6ECF",
      "effectiveDate" : "2015-04-30T23:59:59Z",
      "priceDimensions" : {
        "DQ578CGN99KG6ECF.38NPMPTW36.2TG2D8R56U" : {
          "rateCode" : "DQ578CGN99KG6ECF.38NPMPTW36.2TG2D8R56U",
          "description" : "Upfront Fee",
          "unit" : "Quantity",
          "pricePerUnit" : {
            "USD" : "16924"
          },
          "appliesTo" : [ ]
        },
        "DQ578CGN99KG6ECF.38NPMPTW36.6YS6EN2CT7" : {
          "rateCode" : "DQ578CGN99KG6ECF.38NPMPTW36.6YS6EN2CT7",
          "description" : "Windows (Amazon VPC), hs1.8xlarge instance-hours used this month",
          "beginRange" : "0",
          "endRange" : "Inf",
          "unit" : "Hrs",
          "pricePerUnit" : {
            "USD" : "1.0910000000"
          },
          "appliesTo" : [ ]
        }
      },
      "termAttributes" : {
        "LeaseContractLength" : "3yr",
        "OfferingClass" : "standard",
        "PurchaseOption" : "Partial Upfront"
      }
    },
    "DQ578CGN99KG6ECF.NQ3QZPMQV9" : {
      "offerTermCode" : "NQ3QZPMQV9",
      "sku" : "DQ578CGN99KG6ECF",
      "effectiveDate" : "2015-04-30T23:59:59Z",
      "priceDimensions" : {
        "DQ578CGN99KG6ECF.NQ3QZPMQV9.6YS6EN2CT7" : {
          "rateCode" : "DQ578CGN99KG6ECF.NQ3QZPMQV9.6YS6EN2CT7",
          "description" : "USD 0.0 per Windows (Amazon VPC), hs1.8xlarge instance-hour (or partial hour)",
          "beginRange" : "0",
          "endRange" : "Inf",
          "unit" : "Hrs",
          "pricePerUnit" : {
            "USD" : "0.0000000000"
          },
          "appliesTo" : [ ]
        },
        "DQ578CGN99KG6ECF.NQ3QZPMQV9.2TG2D8R56U" : {
          "rateCode" : "DQ578CGN99KG6ECF.NQ3QZPMQV9.2TG2D8R56U",
          "description" : "Upfront Fee",
          "unit" : "Quantity",
          "pricePerUnit" : {
            "USD" : "42860"
          },
          "appliesTo" : [ ]
        }
      },
      "termAttributes" : {
        "LeaseContractLength" : "3yr",
        "OfferingClass" : "standard",
        "PurchaseOption" : "All Upfront"
      }
    },

每当遇到具有结构良好值的变量键时Dictionary<string, ...>可以使用Dictionary<string, ...>

var obj = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var offer in obj.Reserved.Values.SelectMany(v => v.Values))
{
    Console.WriteLine(offer.effectiveDate);
    foreach (var priceDim in offer.priceDimensions.Values)
        Console.WriteLine("\t" + priceDim.description);
}

// .....................

public class RootObject
{
    public Dictionary<string, Dictionary<string, Offer>> Reserved { get; set; }
}

public class Offer
{
    public string offerTermCode { get; set; }
    public string sku { get; set; }
    public DateTime effectiveDate { get; set; }
    public Dictionary<string, PriceDimension> priceDimensions { get; set; }
    public TermAttributes termAttributes { get; set; }
}

public class PriceDimension
{
    public string rateCode { get; set; }
    public string description { get; set; }
    public string beginRange { get; set; }
    public string endRange { get; set; }
    public string unit { get; set; }
    public Dictionary<string, string> pricePerUnit { get; set; }
    public object[] appliesTo { get; set; }
}

public class TermAttributes
{
    public string LeaseContractLength { get; set; }
    public string OfferingClass { get; set; }
    public string PurchaseOption { get; set; }
}

演示: https : //dotnetfiddle.net/Nj7lz2

注意我假设您使用JSON.Net在C#中处理JSON。

暂无
暂无

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

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