1

So i am trying to get the values from this JSON file the json file

{ "Global Quote": { "01. symbol": "IBM", "02. open": "125.8300", "03. high": "126.2500", "04. low": "123.4000", "05. price": "124.1500", "06. volume": "3115104", "07. latest trading day": "2020-06-17", "08. previous close": "125.1500", "09. change": "-1.0000", "10. change percent": "-0.7990%" } }

i tried to create class with special paste in visual studio and it gave a result like this:

    public class Rootobject
    {
        public GlobalQuote GlobalQuote { get; set; }
    }

    public class GlobalQuote
    {
        public string _01symbol { get; set; }
        public string _02open { get; set; }
        public string _03high { get; set; }
        public string _04low { get; set; }
        public string _05price { get; set; }
        public string _06volume { get; set; }
        public string _07latesttradingday { get; set; }
        public string _08previousclose { get; set; }
        public string _09change { get; set; }
        public string _10changepercent { get; set; }
    }

        WebClient webClient = new WebClient();

        string result = webClient.DownloadString("URL Source");// the result is the json file

        GlobalQuote m = JsonConvert.DeserializeObject<GlobalQuote>(result);



        Console.WriteLine(m._01symbol);
        Console.WriteLine(m._02open);
        Console.WriteLine(m._03high);

it gave me nothing. i think the problem is that the attributs in the class does not match with JSON file. the problem that the attributs in the JSON file contains space. So i am searching for a solution to get values from JSON file without using class. Can anyone help me please and thanks in advance.

cr7911
  • 11
  • 2
  • Take a look at [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182). Both top-rated answers apply: you could either use a `Dictionary` for `GlobalQuote` or mark the properties in `GlobalQuote` with `JsonProperty` attributes, e.g. `[JsonProperty("01. symbol")] public string Symbol { get; set; }` – dbc Jun 18 '20 at 05:07
  • You also need to deserialize to `Rootobject` not `GlobalQuote`, i.e. `GlobalQuote m = JsonConvert.DeserializeObject(result)?.GlobalQuote;` – dbc Jun 18 '20 at 05:09
  • yes i post the code. thanks for your help! – cr7911 Jun 18 '20 at 05:18
  • You're welcome, glad I could help. – dbc Jun 18 '20 at 05:23

1 Answers1

0

So finally i found a solution to my problem:

public class Rootobject
{
    [JsonProperty(PropertyName = "Global Quote")]
    public GlobalQuote GlobalQuote { get; set; }
}

   public class GlobalQuote
    {

    [JsonProperty(PropertyName = "01. symbol")]
    public string _01symbol { get; set; }

    [JsonProperty(PropertyName = "02. open")]
    public string _02open { get; set; }
    [JsonProperty(PropertyName = "03. high")]
    public string _03high { get; set; }
    [JsonProperty(PropertyName = "04. low")]
    public string _04low { get; set; }
    [JsonProperty(PropertyName = "05. price")]
    public string _05price { get; set; }
    [JsonProperty(PropertyName = "06. volume")]
    public string _06volume { get; set; }
    [JsonProperty(PropertyName = "07. latest trading day")]
    public string _07latesttradingday { get; set; }
    [JsonProperty(PropertyName = "08. previous close")]
    public string _08previousclose { get; set; }
    [JsonProperty(PropertyName = "09. change")]
    public string _09change { get; set; }
    [JsonProperty(PropertyName = "10. change percent")]
    public string _10changepercent { get; set; }
    }

    Rootobject oRootObject = new Rootobject();
    oRootObject = JsonConvert.DeserializeObject<Rootobject>(result);
    Console.Writeline(oRootObject.GlobalQuote._02open);
cr7911
  • 11
  • 2