Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AWS EC2读取报价文件JSON文件_C#_Json_Amazon Web Services_Parsing - Fatal编程技术网

C# AWS EC2读取报价文件JSON文件

C# AWS EC2读取报价文件JSON文件,c#,json,amazon-web-services,parsing,C#,Json,Amazon Web Services,Parsing,我试图解析AWS EC2,读取一个提供文件JSON文件,但在读取它时遇到问题。正如您在JSON文件中看到的,有一个产品列表,JSON文件的键值每次都不同,我无法读取它。 你知道怎么读这个文件吗 "Reserved" : { "DQ578CGN99KG6ECF" : { "DQ578CGN99KG6ECF.HU7G6KETJZ" : { "offerTermCode" : "HU7G6KETJZ", "sku" : "DQ578CGN99KG6ECF",

我试图解析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"
      }
    },

每当遇到结构良好的值的变量键时,可以使用
字典

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; }
}
var obj=JsonConvert.DeserializeObject(json);
foreach(obj.Reserved.Values.SelectMany中的var提供(v=>v.Values))
{
Console.WriteLine(offer.effectiveDate);
foreach(报价中的var priceDim.priceDimensions.Values)
Console.WriteLine(“\t”+priceDim.description);
}
// .....................
公共类根对象
{
公共字典保留{get;set;}
}
公开课
{
公共字符串offerTermCode{get;set;}
公共字符串sku{get;set;}
public DateTime effectiveDate{get;set;}
公共字典{get;set;}
公共术语属性{get;set;}
}
公共类价格维度
{
公共字符串费率代码{get;set;}
公共字符串说明{get;set;}
公共字符串beginRange{get;set;}
公共字符串endRange{get;set;}
公共字符串单元{get;set;}
公共字典pricePerUnit{get;set;}
公共对象[]应用于{get;set;}
}
公共类术语属性
{
公共字符串LeaseContractLength{get;set;}
提供类{get;set;}的公共字符串
公共字符串购买选项{get;set;}
}
演示:

注意,我假设您使用C#处理JSON