-1

I am trying deserialize some JSON in C# using the Newtonsoft.Json package. I have used this package many times but with this JSON there is a numeric key and I am unsure how I should access the data I want. This is what I have tried so far. But no luck. Here is the JSON:

{
  "status": "success",
  "timestamp": 1589242995,
  "data": {
    "156": {
      "rate_USD": 0.0005,
      "rate_BTC": "4.45",
      "code": "XPD",
      "name": "Palladium Ounces",
      "copy_from_fiat_currency_id": null
    },
    "50": {
      "rate_USD": 0.8109,
      "rate_BTC": "6934.38",
      "code": "FKP",
      "name": "Falkland Islands Pound",
      "copy_from_fiat_currency_id": null
    },
    "61": {
      "rate_USD": 7.7505,
      "rate_BTC": "66276.56",
      "code": "HKD",
      "name": "Hong Kong Dollar",
      "copy_from_fiat_currency_id": null
    },
    "64": {
      "rate_USD": 323.9345,
      "rate_BTC": "2770038.41",
      "code": "HUF",
      "name": "Hungarian Forint",
      "copy_from_fiat_currency_id": null
    },
    "5": {
      "rate_USD": 1.7952,
      "rate_BTC": "15351.50",
      "code": "ANG",
      "name": "Netherlands Antillian Guilder",
      "copy_from_fiat_currency_id": null
    },
    "160": {
      "rate_USD": 250.375,
      "rate_BTC": "2141014.63",
      "code": "YER",
      "name": "Yemeni Rial",
      "copy_from_fiat_currency_id": null
    },
    "106": {
      "rate_USD": 121.2424,
      "rate_BTC": "1036771.81",
      "code": "NPR",
      "name": "Nepalese Rupee",
      "copy_from_fiat_currency_id": null
    },
    "173": {
      "rate_USD": 420.8644,
      "rate_BTC": "3598908.30",
      "code": "KZT",
      "name": "Kazakhstani Tenge",
      "copy_from_fiat_currency_id": null
    },
    "93": {
      "rate_USD": 7.9839,
      "rate_BTC": "68272.29",
      "code": "MOP",
      "name": "Macau Pataca",
      "copy_from_fiat_currency_id": null
    },
    "74": {
      "rate_USD": 0.709,
      "rate_BTC": "6062.82",
      "code": "JOD",
      "name": "Jordanian Dinar",
      "copy_from_fiat_currency_id": null
    },
    "94": {
      "rate_USD": 357,
      "rate_BTC": "3052789.11",
      "code": "MRO",
      "name": "Mauritanian Ouguiya",
      "copy_from_fiat_currency_id": null
    },
    "140": {
      "rate_USD": 6.7627,
      "rate_BTC": "57829.00",
      "code": "TTD",
      "name": "Trinidad and Tobago Dollar",
      "copy_from_fiat_currency_id": null
    },
    "124": {
      "rate_USD": 9.8744,
      "rate_BTC": "84438.00",
      "code": "SEK",
      "name": "Swedish Krona",
      "copy_from_fiat_currency_id": null
    },
    "132": {
      "rate_USD": 8.7516,
      "rate_BTC": "74836.60",
      "code": "SVC",
      "name": "El Salvador Colon",
      "copy_from_fiat_currency_id": null
    },
    "25": {
      "rate_USD": 2.016,
      "rate_BTC": "17239.21",
      "code": "BZD",
      "name": "Belize Dollar",
      "copy_from_fiat_currency_id": null
    }
  }
}

I am trying to access the rate_USD from each array.

Here is my code:

class Currency
{
    public Data data { get; set;}
}

public class Data
{
    public MyModel mymodel { get; set; }
}

public class MyModel
{
    [JsonProperty("2")]
    public double rate_USD { get; set; }
}


var currencyJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<Currency>(strJSON);

Console.WriteLine(currencyJSON.data.mymodel.rate_USD);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
matthewoak
  • 49
  • 9

1 Answers1

1

I generally use a Dictionary<string, MyModel> for performing such operations and then use the get methods of the properties to pull data from the Dictionary

So your updated class would look something like:

class Currency
{
    public Dictionary<string, MyModel> data { get; set;}

    public MyModel Two
    {
        get 
        {

            if (data == null || !data.ContainsKey("2"))
                return null;
            return data["2"];
        }
    }
}
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71