[英]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.