0
{
    "name": "India",
    "topLevelDomain": [".in"],
    "alpha2Code": "IN",
    "alpha3Code": "IND",
    "callingCodes": ["91"],
    "capital": "New Delhi",
    "altSpellings": ["IN", "Bhārat", "Republic of India", "Bharat Ganrajya"],
    "subregion": "Southern Asia",
    "region": "Asia",
    "population": 1380004385,
    "latlng": [20.0, 77.0],
    "demonym": "Indian",
    "area": 3287590.0,
    "gini": 35.7,
    "timezones": ["UTC+05:30"],
    "borders": ["AFG", "BGD", "BTN", "MMR", "CHN", "NPL", "PAK", "LKA"],
    "nativeName": "भारत",
    "numericCode": "356",
    "flags": {
      "svg": "https://flagcdn.com/in.svg",
      "png": "https://flagcdn.com/w320/in.png"
    },
    "currencies": [{ "code": "INR", "name": "Indian rupee", "symbol": "₹" }],
    "languages": [
      {
        "iso639_1": "hi",
        "iso639_2": "hin",
        "name": "Hindi",
        "nativeName": "हिन्दी"
      },
      {
        "iso639_1": "en",
        "iso639_2": "eng",
        "name": "English",
        "nativeName": "English"
      }
    ],
    "translations": {
      "br": "Índia",
      "pt": "Índia",
      "nl": "India",
      "hr": "Indija",
      "fa": "هند",
      "de": "Indien",
      "es": "India",
      "fr": "Inde",
      "ja": "インド",
      "it": "India",
      "hu": "India"
    },
    "flag": "https://flagcdn.com/in.svg",
    "regionalBlocs": [
      {
        "acronym": "SAARC",
        "name": "South Asian Association for Regional Cooperation"
      }
    ],
    "cioc": "IND",
    "independent": true
  }

This is my JSON data.From this I need to convert

  • name
  • population
  • area
  • altSpellings

these values to c#

Charlieface
  • 52,284
  • 6
  • 19
  • 43
Saranya
  • 11
  • 1

2 Answers2

0

Using the Newtonsoft.Json library you can load the JSON content into a JObject and treat it like a dictionary where the property names are the key values like so:

//Load the JSON into a JObject
var json = ...
var jsonObject = JObject.Parse(json);

//Pull out the property values using the name as a key
var name = jsonObject["name"].ToString();
var population = int.Parse(jsonObject["population"].ToString());
var area = jsonObject["area"].ToString();
var altSpellings = new List<string>();

foreach (var altSpelling in (JArray)jsonObject["altSpellings"])
    altSpellings.Add(altSpelling.ToString());

This is a quick way to get the values out without having to create concrete types to deserialise into. If you happen to have a class that maps onto this JSON structure (json2csharp can be handy here) then you could instead use JsonConvert.DeserializeObject<T> instead to deserialise into an instance of that type where you can access the fields directly.

lee-m
  • 2,269
  • 17
  • 29
0

you can create a class with the properties you need

using Newtonsoft.Json;

Data data = JsonConvert.DeserializeObject<Data>(json);

string name = data.Name; // India

result

{
  "name": "India",
  "population": 1380004385,
  "area": 3287590,
  "altSpellings": [
    "IN",
    "Bhārat",
    "Republic of India",
    "Bharat Ganrajya"
  ]
}

class

public partial class Data
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("population")]
    public long Population { get; set; }
    
    [JsonProperty("area")]
    public long Area { get; set; }

    [JsonProperty("altSpellings")]
    public List<string> AltSpellings { get; set; }

}
Serge
  • 40,935
  • 4
  • 18
  • 45